使用C 中的Parent类构造仪启动器列表
Using parent class constructor initialiser-list in C++
原始帖子
编译后,以下代码会产生错误C2436:'__ctor':成员函数或构造函数初始器列表中的嵌套类
in child.h
#include Parent.h
class Child : public Parent
{
public:
Child (List* pList)
: Parent::Parent(pList)
{
}
};
在这里,父级:
in parent.h
class __declspec(dllimport) Parent : public GrandParent
{
public:
Parent (List* pList = NULL);
}
在parent.cpp
中Parent::Parent (List* pList)
:
m_a(1)
,m_b(1)
,GrandParent(pList)
{
}
使孩子班级打电话给父班构造函数是否正确?
ps如果我将儿童构造函数的声明和实施分为.h和.h和.cpp,也会发生同样的情况。我无法更改父类代码,因为它是预编译库的一部分。
在您的建议之后,@mape,@mat,@barry,@praetorian
我意识到问题是由于父类中存在另一个构造函数。多亏了您的建议,我生成了代码,在儿童类中的新帖子管理多个构造函数(带有初始器列表(中的新帖子管理中的错误(最小,完整和可验证(
根据您的示例进行了改编,这会填充罚款。 Parent::Parent
应该只是Parent
。
#define NULL 0
struct List;
class GrandParent
{
public:
GrandParent(List* pList = NULL) {}
};
class Parent : public GrandParent
{
public:
Parent(List* pList = NULL);
int m_a;
int m_b;
};
Parent::Parent(List* pList)
:
m_a(1)
,m_b(1)
,GrandParent(pList)
{
}
class Child : public Parent
{
public:
Child (List* pList)
: Parent(pList)
{
}
};
int main(void)
{
GrandParent grandparent(NULL);
Parent parent(NULL);
Child child(NULL);
return 0;
}
相关文章:
- Pybind11:将元组列表从Python传递到C++
- 从链接列表c++中删除一个项目
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- C++如何通过用户输入删除列表元素
- 读取文件的最后一行并输入到链接列表时出错
- 复制列表初始化的隐式转换的等级是多少
- LNK2038、MSVS2017 MAGMA的原因列表
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 没有为自己的结构调用列表推回方法
- 如何创建一个空的全局类并在启动时实例化它
- 使用简单类型列表实现的指数编译时间.为什么
- CMake 库目标并不总是出现在 Visual Studio 2017 的"启动项"下拉列表中。
- 在QT中启动程序时如何创建列表或对象
- 重载>>运算符并使用自定义类型的初始值设定项列表启动
- C++列表迭代器如果列表更改则重新启动操作
- 邮件程序启动时如何快速显示邮件列表
- 构造函数中成员启动列表的计算顺序
- [C++]:关于构造函数中的启动列表的问题
- 在C++链接列表中帮助.show记录功能启动无限循环
- 在参数列表中使用绝对路径启动QProcess