开发者

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


If you want to call a base constructor with arguments.


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) {}
};


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜