Jump to content
  • Advertisement
Sign in to follow this  
jujumbura

C++ Polymorphism question

This topic is 4588 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

Hello all, I have a question regarding the construction/destruction of publicly inherited objects in C++. I am pretty comfortable overriding superclass methods with the "virtual" keyword, and I can also easily call the superclass' original method ( if I need to do so, as part of a proccess ) with a Superclass::Method() call, from within the subclass. But what I don't understand is how to achieve similar results from the constructors and destructors. If I have a proccess that I want EVERY subclass to do upon creation, I'd like to put that code in the constructor of the superclass, and then call that constructor from within the constructor of all the subclasses. But when I do something like: Subclass::Subclass() { Superclass::Superclass(); } ... it looks like it creates an instance of the superclass on the stack, and then just tosses it. In other words, it treats it like any other instantiation of a superclass. What I WANT is to just call the superclass' constructor code on the INSTANCE of the subclass being created. Same goes for destructors. Is this possible? Or do I just need to make a seperate method and call it? Thanks much for any advice, jujumbura

Share this post


Link to post
Share on other sites
Advertisement
Actually, you don't have to do anything but implement the default constructor in your base class. Unlike in Java, it is implicitly called on construction of the base class part of your object which is before the more specific c'tors are called.

Share this post


Link to post
Share on other sites
No kidding!

So, what order does it happen in? The order of inheritance? So like, if you have

class B : public A

class C : public B


Then the constructor code will execute A's first, then B's, then C's?

Share this post


Link to post
Share on other sites
Yes. Also, if you have parameters, it is done like so...

Subclass::Subclass( int param ) : SuperClass( param, NULL )
{
// Stuff done just for subclass
}

Share this post


Link to post
Share on other sites
Quote:
Original post by jujumbura
No kidding!

So, what order does it happen in? The order of inheritance? So like, if you have

class B : public A

class C : public B


Then the constructor code will execute A's first, then B's, then C's?


Quote:
From MSDN: Construction and Inheritance
An object of derived type is constructed from the base class to the derived class by calling the constructors for each class in order. Each class constructor can rely on its base classes being completely constructed.


So the constructors in this case would go in the order A, B, C.

Share this post


Link to post
Share on other sites
Why don't you just put soem strings in each of the constructors and destructors and see what happens.

Dave

Share this post


Link to post
Share on other sites
Quote:

Yes. Also, if you have parameters, it is done like so...

Subclass::Subclass( int param ) : SuperClass( param, NULL )
{
// Stuff done just for subclass
}


This is important to know, because the solution of implementing the default constructor isn't always feasible.

Share this post


Link to post
Share on other sites
Don't be tempted to call virtual functions from within a superclass constructor! You'll get strange errors, as the vtable isn't complete at that point. It's better practice to call a separate initialisation method in this case.

Share this post


Link to post
Share on other sites
It should be noted that the destructors unwind in the opposite direction. The most inherited destructor gets called first then it continues on up the chain.

Share this post


Link to post
Share on other sites
Quote:
Original post by PMSchuss
It should be noted that the destructors unwind in the opposite direction. The most inherited destructor gets called first then it continues on up the chain.


Only if the destructors are virtual, of course. That's why you need to use vurtual destructors.

Regards,

Share this post


Link to post
Share on other sites
Sign in to follow this  

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