It's possible to add members to a derived struct
that shadow same-name members of the base class, whether this is an error or not is addressed by another Q&A. My question is about initializing hidden inherited members. By-value assignment of derived D
to base class B
is permitted by the is-a relationship, but it's hard to initialize the inherited member that corresponds to the base class. In the following example, initialization is done with a brace-enclosed initializer list for B
but this is not possible for D
. The only option I found for getting defined D
instances was to default-initialize (zero) and assign afterwards:
#include <iostream>
using namespace std;
int main()
{
struct B { int x; };
struct D: B { int x; };
B bas = {1};
cout << bas.x << endl;
D der = {};
der.x = 2;
bas = der;
cout << bas.x << endl;
der.B::x = 3;
bas = der;
cout << bas.x << endl;
static_cast<B&>(der).x = 4;
bas = der;
cout << bas.x << endl;
return 0;
}
output:
1
0
3
4
The following ideas, which I assumed might work, resulted in compiler errors:
D der = {1, 2};
error: could not convert '{1, 2}' from '<brace-enclosed initializer list>' to 'main()::D'
D der = {B::x=1, 2};
error: invalid use of non-static data member 'main()::B::x'
error: could not convert '{<expression error>, 2}' from '<brace-enclosed initializer list>' to 'main()::D'
(g .exe -Weffc -pedantic -Wextra -Wall -std=gnu 11 -std=c 11 -fexceptions -g -std=c 11
with gcc 8.1.0)
Is there a way for proper initialization in C 11?
Addendum:
When I asked this question, I was so focused on the difficulties to access shadowed members that I totally missed the limited options for aggregate initialization in C 11. So answers to this question will also apply to derived classes without hidden data members.
CodePudding user response:
D der = {1, 2}
would work in C 17, as
D der = {{42}, 2};
For previous version, you cannot initialize base like that and you need a constructor.