[英]What happens if you don't call a base class's constructor for different derived constructors with different signatures?
Say I have a base and derived class like this:假设我有一个 base 和 derived class 这样的:
class Base {
public:
Base() { ... };
Base(int param) { ... };
};
class Derived : public Base {
public:
Derived() { ... };
Derived(int param) { ... };
Derived(int p1, int p2) { ... };
};
Note that I'm not explicitly calling any of Base
's constructors for Derived
's constructors.请注意,我没有为Derived
的构造函数显式调用Base
的任何构造函数。 That is to say, I didn't write Derived(): Base() {... }
or Derived(int param): Base(param) {... }
.也就是说,我没有写Derived(): Base() {... }
或Derived(int param): Base(param) {... }
。
Do any of Base
's constructors get called by default when creating an instance of Derived
?创建Derived
的实例时,默认情况下会调用Base
的任何构造函数吗?
If so, does Base(int param)
get called when using Derived(int param)
, or does Base()
get called?如果是这样,是在使用Derived(int param)
时调用Base(int param)
) 还是调用Base()
?
Put another way, does C++ always default to using a base class's constructor with the same signature as the derived class's constructor if you don't specify which constructor to use?换句话说,如果您不指定要使用哪个构造函数,C++ 是否总是默认使用与派生类的构造函数具有相同签名的基类构造函数? Or does it just use the base class's default constructor?或者它只是使用基类的默认构造函数?
If the former, what about when using a constructor in the derived class that doesn't have a matching constructor with the same signature in the base class, such as Derived(int p1, int p2)
?如果是前者,那么在派生的 class 中使用构造函数时,如果在基类 class 中没有具有相同签名的匹配构造函数,例如Derived(int p1, int p2)
怎么办?
Please note this question does not relate to initialization of member variables of either class. I intentionally did not include any member variables in my pseudo-code.请注意这个问题与 class 的成员变量的初始化无关。我有意没有在我的伪代码中包含任何成员变量。 It specifically has to do with which constructor on the base class gets used if you do not explicitly specify a base constructor in the derived class's constructors.如果您没有在派生类的构造函数中显式指定基构造函数,它特别与使用基 class 上的哪个构造函数有关。
Quoting from cppreference's description of constructors and how inheritance relates to them:引用 cppreference 对构造函数的描述以及 inheritance 与它们的关系:
Before the compound statement that forms the function body of the constructor begins executing, initialization of all direct bases , virtual bases, and non-static data members is finished.在构造函数主体forms function 复合语句开始执行之前,所有直接基类、虚基类和非静态数据成员的初始化已经完成。 Member initializer list is the place where non-default initialization of these objects can be specified.成员初始化器列表是可以指定这些对象的非默认初始化的地方。 For bases and non-static data members that cannot be default-initialized, such as members of reference and const-qualified types, member initializers must be specified.对于不能默认初始化的基类和非静态数据成员,例如引用成员和 const 限定类型,必须指定成员初始化器。 No initialization is performed for anonymous unions or variant members that do not have a member initializer.不对没有成员初始值设定项的匿名联合或变体成员执行任何初始化。
(Emphasis added.) (强调已添加。)
If you do not specify an initialization of the base class subobject in your derived class's constructor's member initializer list, the base class subobject is default-initialized.如果您没有在派生类的构造函数的成员初始化列表中指定基类 class 子对象的初始化,则基类 class 子对象将被默认初始化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.