初始化作为另一个 ISO C++成员的结构
Initializing a struct which is a member of another ISO C++
我有两个结构:
struct port
{
bool isOutput;
bool isConnected;
int connwires;
};
struct node
{
port p;
vector<Wire*> w;
};
我有:
node *nodes;
在我的课堂上。问题是如何初始化由以下人员创建的所有n个节点结构的端口成员(p):
nodes= new node[n];
类构造函数中的语句。
(我像这样定义端口结构:
struct port
{
bool isOutput=0;
bool isConnected=0;
int connwires=0;
};
但它在"ISO C++"中无效。
谢谢。
您需要
提供一个默认构造函数,以便port
自动初始化其成员
struct port
{
port() :
isOutput(false),
isConnected(false),
connwires(0)
{ }
bool isOutput;
bool isConnected;
int connwires;
};
请注意,您的最后一个代码是有效的,并且自 C++11 以来执行了您的预期。
相关文章:
- 如何在C++中定义静态成员结构
- 使用模板化类的成员结构
- 初始化固定的 C 数组成员结构
- 指向成员结构的指针如何工作
- 如何将此“指针从外部类传递到成员结构
- 导致IAR ARM中出现错误的成员结构位字段元素的Initializer列表初始化
- 映射文件中成员结构的地址
- Visual C++ 模板类成员结构初始化语法糖
- C :初始化成员结构的静态字段的正确方法
- 如何在C 类的初始化器列表中使用未命名结构初始化成员结构
- 从成员结构的成员函数中访问类的成员?
- 模板化类的成员函数无法返回指向成员结构的指针?
- 重载非类型模板结构的成员结构的复制赋值运算符
- 灵气属性传播问题与单成员结构.
- 将具有 int* 成员C++结构编接到 C#
- 复杂的班级成员结构
- 将向量成员(结构)传递给函数
- 成员结构的前向声明
- 重载运算符<<用于模板结构中定义的成员结构
- 返回带有模板化类的类成员结构