Jump to content
  • Advertisement

Archived

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

Pixel_Sticks

Question abotu constructors and destructors.

This topic is 5801 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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
{
}

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
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

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!