The structure initializer can be used in C ++, but only in the pre-C99 style (i.e. you cannot use the assigned initializers). Assigned initializers have been introduced in C99 that allow you to specify elements that will be initialized by name, rather than relying on the declaration order, but are not part of any C ++ standard at the moment (adding a general assumption that C ++ is superset of C).
If you want to write non-portable C ++ code specifically designed for g ++, you can always use the GCC extension , which has the same functionality as the designated constructors. The syntax is as follows:
struct value_t my_val = { member_a: 1, member_b: 1.2f };
This link gives a pretty good overview of both types of initialization in the context of C.
Here is an excerpt that shows both the previous ones (without notation) and the C99 styles:
When initializing the structure, the first initializer in the list initializes the first declared element (if it is not specified (starting from C99), and all subsequent initializers without (starting from C99) initialize the elements of the structure declared after the one that was initialized by the previous expression.
struct point {double x,y,z;} p = {1.2, 1.3};
In some cases, you may need to write code to initialize the structure, in which case you can use the result of a function, for example:
struct Resources AndroidTimerModel::ResData = function_that_acts_like_a_constructor();
source share