如何指定继承类的构造函数定义?

时间:2012-03-01 17:24:34

标签: c++ oop inheritance

我有一个基类(标题):

class BaseClass
{
    public:
        BaseClass(int); // note no default constructor.
}

和派生它的类:

class DerivedClass : public BaseClass
{
    public:
        DerivedClass(void);
}

DerivedClass::DerivedClass (void) // note SPECIFICALLY no parameters.
{
    super (10); // equivalent of what I am trying to do in Java (I think)
}

如何在派生类中调用继承类的构造函数?

3 个答案:

答案 0 :(得分:1)

像这样:

DerivedClass::DerivedClass (void) : BaseClass(10) 
{ }

注意:如果您不熟悉语法,请留意成员初始化列表

答案 1 :(得分:1)

class BaseClass 
{ 
    public: 
        BaseClass(int) {};
} ;
class DerivedClass : public BaseClass 
{ 
    public: 
        DerivedClass(void); 
}; 

DerivedClass::DerivedClass (void)
:BaseClass(10)
{ 

}

答案 2 :(得分:0)

这就是你想要的:

DerivedClass(void):BaseClass(10) {
    //blah blah blah
}