Or, create an instance and initialize it at the same time:
struct test mytest = { 1, 22 };
Now, is it possible to use Method B, initializing just a bunch of members in a random sequence (example given doesn't work - just for illustration what I mean...)???
1 2
struct test mytest = { b = 22 }; //which fails since b is not defined here...
struct test mytest2 = { b = 22, a = 1 };
Tested this on GCC 4.8.0. Works fine with a warning:
designated_initializer.cpp: In function 'int main()':
designated_initializer.cpp:11:14: warning: ISO C++ does not allow C99 designated initializers [-Wpedantic]
Test t = {.i = 1, .c = 'a'};
^
designated_initializer.cpp:11:21: warning: ISO C++ does not allow C99 designated initializers [-Wpedantic]
Test t = {.i = 1, .c = 'a'};
^
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
struct Test
{
int i;
char c;
};
int main()
{
Test t = {.i = 1, .c = 'a'};
std::clog << "t.i == " << t.i << '\t' << "t.c == " << t.c << '\n';
}