为什么我的 C++子类需要显式构造函数吗?
我有一个声明并定义构造函数的基类,但由于某种原因,我的公共派生类看不到该构造函数,因此我必须在派生类中显式声明一个转发构造函数:
class WireCount0 {
protected:
int m;
public:
WireCount0(const int& rhs) { m = rhs; }
};
class WireCount1 : public WireCount0 {};
class WireCount2 : public WireCount0 {
public:
WireCount2(const int& rhs) : WireCount0(rhs) {}
};
int dummy(int argc, char* argv[]) {
WireCount0 wireCount0(100);
WireCount1 wireCount1(100);
WireCount2 wireCount2(100);
return 0;
}
在上面的代码中,我的 WireCount1wireCount1(100)
声明被编译器拒绝(“没有匹配的函数用于调用 'WireCount1::WireCount1(int)'”),而我的 wireCount0
和 wireCount2
声明没问题。
我不确定我是否理解为什么需要提供 WireCount2 中所示的显式构造函数。是否是因为编译器为 WireCount1
生成了默认构造函数,而该构造函数隐藏了 WireCount0
构造函数?
作为参考,编译器为 i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5659)
。
I have a base class that declares and defines a constructor, but for some reason my publicly derived class is not seeing that constructor, and I therefore have to explicitly declare a forwarding constructor in the derived class:
class WireCount0 {
protected:
int m;
public:
WireCount0(const int& rhs) { m = rhs; }
};
class WireCount1 : public WireCount0 {};
class WireCount2 : public WireCount0 {
public:
WireCount2(const int& rhs) : WireCount0(rhs) {}
};
int dummy(int argc, char* argv[]) {
WireCount0 wireCount0(100);
WireCount1 wireCount1(100);
WireCount2 wireCount2(100);
return 0;
}
In the above code, my WireCount1 wireCount1(100)
declaration is rejected by the compiler ("No matching function for call to 'WireCount1::WireCount1(int)'"), while my wireCount0
and wireCount2
declarations are fine.
I'm not sure that I understand why I need to provide the explicit constructor shown in WireCount2
. Is it because the compiler generates a default constructor for WireCount1
, and that constructor hides the WireCount0
constructor?
For reference, the compiler is i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5659)
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
构造函数不被继承。您必须为派生类创建一个构造函数。此外,派生类的构造函数必须调用基类的构造函数。
Constructors are not inherited. You have to create a constructor for the derived class. The derived class's constructor, moreover, must call the base class's constructor.
所有派生类必须以某种形式调用其基类的构造函数。
当您创建重载构造函数时,默认编译器生成的无参数构造函数将消失,并且派生类必须调用重载构造函数。
当你有这样的东西时:
编译器实际上生成这个:
当你有非默认构造函数时,不再生成无参数构造函数。当您定义以下内容时:
编译器不再在 Class1 中生成构造函数来调用基类的构造函数,您必须自己显式执行此操作。
All the derived classes must call their base class's constructor in some shape or form.
When you create an overloaded constructor, your default compiler generated parameterless constructor disappears and the derived classes must call the overloaded constructor.
When you have something like this:
The compiler actually generates this:
When you have non-default constructor, the parameterless constructor is no longer generated. When you define the following:
The compiler no longer generates a constructor in Class1 to call the base class's constructor, you must explicitly do that yourself.
在处理派生类之前,您必须先构造基类。如果您使用非平凡的构造函数构造派生类,则编译器无法决定调用基类的内容,这就是发生错误的原因。
You have to construct your base class before dealing with derived. If you construct your derived class with non-trivial constructors, compiler cannot decide what to call for base, that's why error is occuring.