I would like to initilize some elements of an struct and array in C++.
In C you can do:
unsigned char array[30] = {[1] = 4, [20] = 4};
struct mystruct
{ int i;
int j;
}
struct mystruct e = {.j = 2};
But I cannot do it in C++. Is there any way to implement this kind of designated initializers?
在C++
struct有构造函数(就像class
一样),因此你总是可以在其中初始化var。
Its always good to Initialize ALL the element in array or structure to avoid many errors.
Below may help you.
Initialization for struct
struct myStruct
{
int i;
int j;
myStruct()
{
j=10; //default Constructor
}
};
Initialization for Array:
unsigned char array[5];
array[0]='A';
array[2]='C';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.