C++ equivalent of super(...)

Started by
6 comments, last by tsuraan 21 years, 3 months ago
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.
Advertisement
Yes, you can do the following:

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.
Domine non secundum peccata nostra facias nobis
Ok, thanks. That helps
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
...unless you want to use something else than the default constructor.
You do if you need to pass parameters to its constructor.
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
Yeah, my problem was that I wanted to extend ifstream and ofstream so that the write and read commands could accept any type of data, rather than just char*. So, being able to call the non-default constructor is rather important... It''s working well now though, so thanks!

This topic is closed to new replies.

Advertisement