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.
[class.access.base]/5
... 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.
Yes. These are extract of the c++ standard grammar rules:
id-expression:
unqualified-id
qualified-id
postfix-expression:
[...]
postfix-expression . template[opt] id-expression
[...]
In [class.mcft.non-static]:
When an id-expression (8.1) that is not part of a class member access syntax (8.2.5) and not used to form
a pointer to member (8.3.1) is used in a member of class X in a context where this can be used (8.1.2), if
name lookup (6.4) resolves the name in the id-expression to a non-static non-type member of some class
C, and if either the id-expression is potentially evaluated or C is X or a base class of X, the id-expression is
transformed into a class member access expression (8.2.5) using (*this) (12.2.2.1) as the postfix-expression
to the left of the . operator.