Quantcast
Viewing latest article 9
Browse Latest Browse All 22

Answer by Nils Pipenbrinck for What are the rules for calling the base class constructor?

If you have a constructor without arguments it will be called before the derived class constructor gets executed.

If you want to call a base-constructor with arguments you have to explicitly write that in the derived constructor like this:

class base{  public:  base (int arg)  {  }};class derived : public base{  public:  derived () : base (number)  {  }};

You cannot construct a derived class without calling the parents constructor in C++. That either happens automatically if it's a non-arg C'tor, it happens if you call the derived constructor directly as shown above or your code won't compile.


Viewing latest article 9
Browse Latest Browse All 22

Trending Articles