ssb
ssb

Reputation: 209

About base class in derived class initialization

guys. I see several cases like:

class Derived:public Base{

public:
    Derived(...):Base(...){}
}

Is what situation or is there any principle that we should explicitly initialize the Base in the Derived ctor initialization list? Thanks

Upvotes: 1

Views: 428

Answers (5)

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272457

If you want to call a base constructor with arguments.

Upvotes: 4

BЈовић
BЈовић

Reputation: 64203

A good coding standard will recommend you to always initialize base class in the constructor's initialization list.

If the constructor of the base class requires some arguments, then you have to do it.

Upvotes: 0

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31722

If you have multiple constructor in your base class (basically entry point), So you have choice to call any of them.

Upvotes: 0

Mahesh
Mahesh

Reputation: 34615

In case if we need to pass the arguments of derived constructor to the base constructor, it can be used.

class foo
{
    public:
        foo() {} 
        foo( int num ) {}
};

class bar : public foo
{
    public:
        bar(int barNum): foo(barNum) {}
};

Upvotes: 0

mik
mik

Reputation: 39

If don't explitily initialize the base class, the default constructor will be called.

Upvotes: 0

Related Questions