简体   繁体   中英

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

如果要使用参数调用基础构造函数。

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

如果您的基类中有多个构造函数(基本上是入口点),那么您可以选择调用它们中的任何一个。

如果不能初始化基类,则将调用默认构造函数。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM