从派生类调用基类构造函数的C++规则是什么?
例如,我知道在Java中,您必须作为子类构造函数的第一行执行此操作(如果不这样做,则假定对无参数超级构造函数的隐式调用-如果缺少,则会给您一个编译错误)。
从派生类调用基类构造函数的C++规则是什么?
例如,我知道在Java中,您必须作为子类构造函数的第一行执行此操作(如果不这样做,则假定对无参数超级构造函数的隐式调用-如果缺少,则会给您一个编译错误)。
当前回答
如果在基构造函数中有默认参数,则将自动调用基类。
using namespace std;
class Base
{
public:
Base(int a=1) : _a(a) {}
protected:
int _a;
};
class Derived : public Base
{
public:
Derived() {}
void printit() { cout << _a << endl; }
};
int main()
{
Derived d;
d.printit();
return 0;
}
输出为:1
其他回答
如果基类构造函数没有参数,它们将自动为您调用。如果要使用参数调用超类构造函数,则必须使用子类的构造函数初始化列表。与Java不同,C++支持多重继承(无论好坏),因此基类必须按名称引用,而不是“super()”。
class SuperClass
{
public:
SuperClass(int foo)
{
// do something with foo
}
};
class SubClass : public SuperClass
{
public:
SubClass(int foo, int bar)
: SuperClass(foo) // Call the superclass constructor in the subclass' initialization list.
{
// do something with bar
}
};
有关构造函数初始化列表的更多信息,请点击此处。
CDerived::CDerived()
: CBase(...), iCount(0) //this is the initialisation list. You can initialise member variables here too. (e.g. iCount := 0)
{
//construct body
}
如果您有一个没有参数的构造函数,则在执行派生类构造函数之前将调用它。
如果要使用参数调用基构造函数,则必须在派生构造函数中显式地这样写:
class base
{
public:
base (int arg)
{
}
};
class derived : public base
{
public:
derived () : base (number)
{
}
};
如果不调用C++中的父构造函数,就无法构造派生类。如果它是非参数C',则会自动发生这种情况;如果您直接调用派生构造函数(如上所示),或者代码不会编译,则会发生这种情况。
当一个类从多个类派生时,没有人提到构造函数调用的顺序。该序列如派生类时所述。
向父构造函数传递值的唯一方法是通过初始化列表。初始化列表是用:实现的,然后是要传递给该类构造函数的类和值的列表。
Class2::Class2(string id) : Class1(id) {
....
}
还要记住,如果您有一个在父类上不接受参数的构造函数,那么它将在子构造函数执行之前被自动调用。