简体   繁体   English

关于派生类初始化中的基类

[英]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? 是什么情况或者是否有任何原则我们应该在Derived ctor初始化列表中显式初始化Base? 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. 如果基类的构造函数需要一些参数,那么你必须这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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