通过使用范围解析避免多重继承引起的歧义

Avoid multiple inheritance induced ambiguity by using scope resolution

这是一个多重继承的例子。我使用范围解析运算符来解决歧义而不是虚拟 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;
}

B::i 格式正确吗?

Is B::i well-formed?

是的,是的。最相关的参考是 [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.

这表明您可以命名 i,因为它是 B 基地的成员。可访问性仅在事后检查,在您的情况下是 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.

是的。这些是 c++ 标准语法规则的摘录:

id-expression:
  unqualified-id
  qualified-id

postfix-expression:
  [...]
  postfix-expression . template[opt] id-expression
  [...]

在[class.mcft.non-静态]中:

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.