C++-c++子类初始化式的形式调用父类构造函数的奇怪问题
例如
class Base
{
public:
Base(int i, int j);
};
Base::Base(int i, int j)
{
}
//---
class Child : public Base
{
public:
Child(int i):Base(1,1);
};
Child(int i):Base(1,1)
{
}
//---
int main()
{
}
这样写会报错误 4 error C2534: “Child”: 构造函数无法返回值 d:改着玩用的备份myconsole1myconsole1mymain.cpp 56
错误 5 error C2562: “Child::Child”: “void”函数返回值 d:改着玩用的备份myconsole1myconsole1mymain.cpp 56
错误 3 error C2630: “;”在应是逗号分隔列表的位置找到了 d:改着玩用的备份myconsole1myconsole1mymain.cpp 44
错误 1 error C2969: 语法错误 : “;”: 成员函数定义应以“}”结尾 d:改着玩用的备份myconsole1myconsole1mymain.cpp 35
等错误
将//---之间的代码改为
class Child : public Base
{
public:
Child(int i):Base(1,1)
{
}
};
就不会有错,为什么呢?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在子类方法中显示调用基类的构造函数,不应该在声明处出现。因为它是函数定义的一部分