C++ equivalent of super(...)
Is there anything in C++ that one can use to call the constructor of a parent class from within the constructor of a child class? This is similar to Java''s super(...), if anyone is familiar with that.
Yes, you can do the following:
Derived::Derived()
{
Base::Base();
}
In this case Derived inherits from Base.
Derived::Derived()
{
Base::Base();
}
In this case Derived inherits from Base.
class Baseclass{public: Baseclass();};class Derived : public Baseclass{ public: Derived();};// either do this:Derived::Derived() : Baseclass(){ };// or do this:Derived::Derived(){ ::Baseclass(); // or even Baseclass::Baseclass(); // or even this works i think: Baseclass();};
I have spoken.
The constructor of the base class will be called before the constroctor of the derived class. You don''t need to do this yourself.
My Site
My Site
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement