Archived

This topic is now archived and is closed to further replies.

Pixel_Sticks

Question abotu constructors and destructors.

Recommended Posts

Hey all, Have a simple question on Constructors and Destructors. I am new to programming and C++ but i have been doing a lot of reading. I am confused about constructors. I know that you declare the constructor is your class definition(or is it prototype) but anyway like you would declare the constructor in the .h file for your class. I have also read that they are for initializing variables and functions in your class. Instead of using your own constructors why cant you just initialize the variables inside of the .h file where you defined the class? Im sorry if that is not clear, i tried to be. If not let me know and i will give more information. Thanks.

Share this post


Link to post
Share on other sites
You can have more than 1 constructor and they could initialise the variables to different values, so it''s for added flexibility.

I think the C++ standard says you should be able to initialise static class members when you declare them, but that doesn''t work on VC6.

What you should do for constructors is use the member initialisation section to init members, it can be faster than initialising them in the body of the constructor, i.e. do

CClass::CClass()
: m_var1(0),
m_var2(0) // this is the member initialisation section
{
}

Share this post


Link to post
Share on other sites
A constructor forms part of the 2-phase process required to create an object. The 2 phases are:

1. Allocated some storage for the object;
2. Execute the constructor for the object.

As you know, you can create many objects of the same class type, and each of these objects has its own copy of the variables within the class. That means that any attempt to refer to object variables must include a reference to the actual object aswell. If you were to initialise variables within the header file, before you''ve actually created an object, how would you specify which object you are initialising variables for? You can''t. So, the convenient place to initialise variables is within the constructor, where you will always be referring to the variables within the object currently being created.

Share this post


Link to post
Share on other sites
static inside a class means that all classes get the same variable, i.e. any instance reading or writing the variable affects it for all other instances.

You can have static member functions too, they can only access the static members of the class, because they don''t get passed the implicit ''this'' pointer.

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
If you were to initialise variables within the header file, before you''ve actually created an object, how would you specify which object you are initialising variables for? You can''t.

I don''t really see that argument - the logical thing would be to perform the inline initializations before running the constructor body. There would be very little semantical difference between this and using the constructor initializer list.

Incidentally, some other languages do it this way...


"Is God willing to prevent evil, but not able? Then he is not omnipotent. Is he able, but not willing? Then he is malevolent. Is he both able and willing? Then whence cometh evil? Is he neither able nor willing? Then why call him God?"
Epicurus

Share this post


Link to post
Share on other sites
quote:
Original post by Arild Fines
There would be very little semantical difference between this and using the constructor initializer list.



The constructor initializer list can still make use of parameters passed to the constructor, while initialising in the header is non-parametrised (unless you use templates, but that''s a whole other can of worms ).


Share this post


Link to post
Share on other sites
quote:
Original post by Arild Fines
I don''t really see that argument - the logical thing would be to perform the inline initializations before running the constructor body. There would be very little semantical difference between this and using the constructor initializer list.

I took Pixel_sticks question as wanting to know why initialisation has to be done as part of the constructor rather than saying something like:


  
class C
{
int x = 10;
int y = 20;
};


The point is that a "this" has to exist to know which instance variables are being initialised for. The issue of whether initialisation is done in the initialiser list or ctor body is not really relevant ("this" exists before the ctor body).

Share this post


Link to post
Share on other sites
Yeah, I get that part - but my point was that the compiler could basically turn this:

  
class Class
{
int a = 42;
int b = 84;
Class()
{
//lots of funny constructor stuff

]
}

into this:

  
class Class
{
Class()
{
this->a = 42;
this->b = 84;

//lots of funny constructor stuff

]
}


quote:

while initialising in the header is non-parametrised


Ok, that''s a fair point - but if you want inline initialization, you rarely want it parametrized anyhow.

Share this post


Link to post
Share on other sites
If your second code snippet looked like this:

  
class Class
{
Class()
: a(42),
b(84)
{
//lots of funny constructor stuff

}
}


I''d be more inclined to agree with you, else you''re paying for more than what you really need. C++ could do that, but it doesn''t, and I don''t feel that it is useful for a newbie to be exposed to such explanations.

Share this post


Link to post
Share on other sites
As I pointed out earlier, the reason that wouldn''t make sense is because you can have MULTIPLE constructor overloads which initialise the variables to DIFFERENT default values, or you could decide not to initialise anything in certain constructors.

I also said it should be allowed for static class member variables, like in Java, but not all compilers support that (VC6).

Share this post


Link to post
Share on other sites