Quantcast
Channel: What are the rules for calling the base class constructor? - Stack Overflow
Viewing all articles
Browse latest Browse all 22

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

$
0
0

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 all articles
Browse latest Browse all 22

Trending Articles