我有一个基类(标题):
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)
}
如何在派生类中调用继承类的构造函数?
答案 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
}