7

I'm using Visual C++, If I compile this code:

class A {};
class B : private A {};
class C : public B
{
    void func()
    {
        A a{};
    }
};

I get this error:

error C2247: 'A' not accessible because 'B' uses 'private' to inherit from 'A'

I know that if I use private inheritance, Then the members of the class 'A' would be private in 'B', And inaccessible in 'C', But why can't I create an object of 'A' inside 'C'?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
StackExchange123
  • 1,871
  • 9
  • 24

1 Answers1

7

The problem is that the name A inside the scope of the class C is a private name.

It is a so-called injected class name.

From the C++ Standard (6.3.2 Point of declaration)

8 The point of declaration for an injected-class-name (Clause 12) is immediately following the opening brace of the class definition.

Use the following approach that is use the qualified name

class A {};
class B : private A {};
class C : public B
{
    void func()
    {
        ::A a{};
      //^^^^^^ 
    }
};
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335