What is the importance of calling the base class constructor explicitly?

class A {
    A() { }
};

class B : public A {
    B() : A() { }
};

Why do we need to explicitly call the base class constructor inside the constructor B? Isn't that implied?

+3
source share
4 answers

This is implicit. You will need this syntax, if there is a constructor with arguments, this is the way they are passed.

+11
source

This is implicit and unnecessary. If you do not call the constructor of the base class explicitly, the default constructor (one that has no parameters) is used.

There is only the need to explicitly call the constructor if the base class does not have a default constructor, or if you want to call a different constructor than the default constructor.

, .

+8

, A . .

+2

. :

class A 
{
private:
    int m_i;

public:
    A(int i) 
      : m_i(i)
    { 
    } 
}; 
class B:public A 
{ 
public:
    B()
    { 
    } 
};

( ), A . .

, . , .

0

Source: https://habr.com/ru/post/1733899/


All Articles