Trying to create empty constructor for a struct that has union as a member variable. Union also has a struct and an additional variable as member variables.
Took few approaches, but non were successful. How can I create an empty constructor for nested anonymous structures? How can I call the member variables afterwards?
typedef struct test_struct
{
int a;
union {
int b;
struct {
int c;
};
};
} test_struct;
Below won't compile
test_struct() : a(10), b(20), c(30) {
}
Below sets both b and c as 30
test_struct() : a(10), b(20) {
c = 30;
}
Here is the print function
int main(void)
{
test_struct *ts = new test_struct();
printf("%i\n%i\n%i\n", ts->a, ts->b, ts->c);
return 0;
}
I trying to create an empty constructor that will print 10 20 30 with my printf().
EDIT: I received comments on how I cannot initialize union in this way. However, the header file I received (which I cannot change at all) has structs defined in such nested structure. I do not know how the initialization goes in the back, but my code has to print out values for all the member variables. (a, b, and c for the below example code). Any approaches/reading I can do to achieve this?