Here is an example of multiple inheritance. I used the scope resolution operator to resolve the ambiguity instead of a virtual class.
struct A
{
int i;
};
struct B : A
{};
struct C : A
{};
struct D: B, C
{
void f()
{
B::i = 10;
}
void g()
{
std::cout << B::i <<std::endl;
}
};
int main()
{
D d1;
d1.f();
d1.g();
return 0;
}
Is B::i
well-formed?
Is
B::i
well-formed?
Yes, it is. The most pertinent reference is [class.qual]/1:
If the nested-name-specifier of a qualified-id nominates a class, the name specified after the nested-name-specifier is looked up in the scope of the class, except for the cases listed below. The name shall represent one or more members of that class or of one of its base classes.
Which specifies you can name i
on account of it being a member of B
's base. The accessibility is only checked afterwards, and in your case it's public.
... The access to a member is affected by the class in which the member is named. This naming class is the class in which the member name was looked up and found... A member m is accessible at the point R when named in class N if
- there exists a base class B of N that is accessible at R, and m is accessible at R when named in class B.