为什么在C ++中声明结构体时没有直接的分配? 我有以下不能使用C ++编译器的C代码:
static const struct {
struct structtype1 header;
struct {
struct structtype2 intf;
struct structtype3 src;
} foo1, foo2;
} bar = {
.header = {
.byte1 = 0x23,
.length = 12,
.count1 = 4,
.count2 = 4,
},
.foo1= {
.intf = {
.data = 0x23,
.len = 2,
.ep = 3,
},
.src= {
.data = 0x21,
.len = 4,
.ep = 2,
},
},
.foo2= {
.intf = {
.data = 0x17,
.len = 11,
.ep = 2,
},
.src= {
.data = 0x20,
.len = 2,
.ep = 1,
},
},
};
答案 0 :(得分:0)
你有两个选择:
1.使用构造函数
这将定义成员的值,但所有结构都是相同的
struct _pnt { int x,y,z; _pnt() { x=0; y=0; z=0; } };
_pnt p0,p1,p2; // all points are initialized to (0,0,0)
2.像往常一样定义它仍然适用于我使用的任何C ++编译器
不要忘记成员的顺序与声明中的相同 !!!
struct _pnt { int x,y,z; };
_pnt p0={0.0,0.0,0.0},
p1={1.0,0.0,0.0},
p2={0.0,1.0,0.0};
问题是你必须初始化所有成员而不仅仅是一些!!!