[英]Initializing a struct which is a member of another struct - 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以来,您的最后一个代码是有效的,并且可以实现您的期望。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.