Question abotu constructors and destructors.

Started by
11 comments, last by Pixel_Sticks 21 years, 7 months ago
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.
Advertisement
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
{
}
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
hey thanks for the fast reply, i understood everything you said except for one part.

What does static mean? Is that link a constant?

sorry, im new
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.
cool that explains a lot

they are becoming clearer now. I am beginning to think that i am a moron, i have read the classes section of Sams C++ in 21 Days 3 times now
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.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
gotcha, Thanks for the replys guys! :D
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
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
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 ).


It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
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).

This topic is closed to new replies.

Advertisement