关于基类在派生类初始化 [英] About base class in derived class initialization

查看:181
本文介绍了关于基类在派生类初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

家伙。我看到几个例子:

guys. I see several cases like:

class Derived:public Base{

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



什么情况或有什么原则,我们应该显式初始化基地在Derived ctor初始化列表?感谢

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

这篇关于关于基类在派生类初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆