Newbie needs help!
im a newbie and im grasping the basics, but i''m stumped, i was reading about constructors and destructors. WHAT POSSIBLE PURPOSE is there for them?
any help would be awesome
Well let''s see, they let you initialise variables automatically and they also let you clean up.
Example.
class CSomething
{
char *memory;
public:
CSomething() // THe constructor
{
memory = NULL;
}
~CSomething() // THe destructor
{
if(memory != NULL) delete [] memory;
}
// Other functions go here
};
So if your class ends up initialising memory in one of the other functions, when the class goes out of scope (is no longer used) the memory is cleaned up - no memory leak!!
That''s one use, there are others.
Mark Allen
Lead Programmer
Tashco Studios
http://www.tashco.com
Example.
class CSomething
{
char *memory;
public:
CSomething() // THe constructor
{
memory = NULL;
}
~CSomething() // THe destructor
{
if(memory != NULL) delete [] memory;
}
// Other functions go here
};
So if your class ends up initialising memory in one of the other functions, when the class goes out of scope (is no longer used) the memory is cleaned up - no memory leak!!
That''s one use, there are others.
Mark Allen
Lead Programmer
Tashco Studios
http://www.tashco.com
I''m going to assume that you know what constructors and destructors are..but don''t have any idea on why to use them..
While there are quite a few reasons I''m going to just list a couple..
One plus to constructors is easy initialization...
instead of saying
ClassFoobar Foo;
Foo.x=1;
Foo.y=2;
Foo.z=3;
I can say
ClassFoobar Foo(1,2,3);
(BTW my constructor would look like this...)
ClassFoobar::ClassFoobar(int px, int py, int pz)
{
x = px;
y = py;
z = pz;
}
or in the default constructor
I could have all the variables set to default values - such as zero...
So I could say
ClassFoobar Foo;
and Foo.x would equal zero automatically - instead of some random value until I set it with Foo.x = 0;
you can also include lots of other initialization code...
for example I have a sprite class that takes a filename as a parameter in its constructor...
so instead of
kSprite Ship;
Ship.Load("Ship.dat");
I can say
kSprite Ship("Ship.dat");
sure in that example it only saved me one line of typing but I think you can think of maybe a better example...
as for destructors they make cleanup of dynamically allocated memory very easy
like in my destructor I could have..
ClassFoobar::~ClassFoobar()
{
if(ptr_DynamicallyAllocatedMemory)
delete ptr_DynamicallyAllocatedMemory
}
so that when the class went out of scope the cleanup code would automatically be called - I don''t have to remember to call a *.Destroy() or *.Free() function at the end...
BTW - what book are you reading? (If they are going to introduce a topic - you''d at least think they would tell you why its useful...)
hope this helped some...
While there are quite a few reasons I''m going to just list a couple..
One plus to constructors is easy initialization...
instead of saying
ClassFoobar Foo;
Foo.x=1;
Foo.y=2;
Foo.z=3;
I can say
ClassFoobar Foo(1,2,3);
(BTW my constructor would look like this...)
ClassFoobar::ClassFoobar(int px, int py, int pz)
{
x = px;
y = py;
z = pz;
}
or in the default constructor
I could have all the variables set to default values - such as zero...
So I could say
ClassFoobar Foo;
and Foo.x would equal zero automatically - instead of some random value until I set it with Foo.x = 0;
you can also include lots of other initialization code...
for example I have a sprite class that takes a filename as a parameter in its constructor...
so instead of
kSprite Ship;
Ship.Load("Ship.dat");
I can say
kSprite Ship("Ship.dat");
sure in that example it only saved me one line of typing but I think you can think of maybe a better example...
as for destructors they make cleanup of dynamically allocated memory very easy
like in my destructor I could have..
ClassFoobar::~ClassFoobar()
{
if(ptr_DynamicallyAllocatedMemory)
delete ptr_DynamicallyAllocatedMemory
}
so that when the class went out of scope the cleanup code would automatically be called - I don''t have to remember to call a *.Destroy() or *.Free() function at the end...
BTW - what book are you reading? (If they are going to introduce a topic - you''d at least think they would tell you why its useful...)
hope this helped some...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement