Initialize a variable of a derived structure type at compile time in C ++

I have the following structures:

struct B
{
   int a;
   int b;
};

struct D: public B
{
   int c;
};

I want to initialize some struct D variable at compile time, for example, if I initialized structure B, it would look like this:

B b1 = { value_of_a, value_of_b };

I tried to do this in the following ways, but it did not compile:

D d1 = { { value_of_a, value_of_b } , value_of_c };
D d2 = { value_of_a, value_of_b , value_of_c };

If I changed the structure to:

struct D
{
   B bb;
   int c;
};

it compiles with initialization of "d1" and "d2".

So the question is, how can I initialize a derived structure? And if there is a rule for initializing a derived structure, what are the reasons?

Thank.

+3
source share
1 answer

D, B, ( ), , . .

+1

All Articles