Avoid the ambiguity of multiple inheritance using region resolution

Here is an example of multiple inheritance. I used the scope resolution operator to eliminate 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::iwell formed?

+4
source share
2 answers

Is B::iwell formed?

Yes it is. The most appropriate link is [class.qual] / 1 :

, , - , , . .

, i - , B. , .

[class.access.base]/5

... , . - , ... m R, N,

  • B N, R, m R, B.
+5

. ++:

id-expression:
  unqualified-id
  qualified-id

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

[class.mcft.non-static]:

id- (8.1), (8.2.5) (8.3.1) X , (8.1.2), name lookup (6.4) id- - C, id, C X, X, id (8.2.5), (* this) (12.2.2.1) . .

+1

Source: https://habr.com/ru/post/1687144/


All Articles