Skip to main content
gamedev.net
🔒 Locked

C++ Polymorphism question

Started by jujumbura 9 replies 1.3k views
Original Post
jujumbura
jujumbura
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
LtJax
LtJax
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.
jujumbura
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?
Simian Man
Simian Man
Yes. Also, if you have parameters, it is done like so...

Subclass::Subclass( int param ) : SuperClass( param, NULL ){   // Stuff done just for subclass}
Agony
Agony
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.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o
dave
dave
Why don't you just put soem strings in each of the constructors and destructors and see what happens.

Dave
jpetrie
jpetrie
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.
_winterdyne_
_winterdyne_
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.
Winterdyne Solutions Ltd is recruiting - this thread for details!
PMSchuss
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.
Emmanuel Deloget
Emmanuel Deloget
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,
Polymorphic OOP
Polymorphic OOP
Quote:
Original post by Emmanuel Deloget
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.


No, not only when they are virtual. You only need a virtual destructor if you are deleting the object via a pointer to the base. Destruction always occurs from most-derived to bases regardless (and virtual bases always last).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.