Private inheritance, reference to a static member of the base class

I have a simple question regarding inheritance from a class that privately inherits a base class, i.e.

class Base {};
class Heir: private Base {};
class HeirsHeir : public Heir {};

In the understanding that HeirsHeirthey can’t get access to "their" Base. In particular, it cannot have a method that returns a Base &reference to itself. But why can't it return a link to another Base object? So why the following code does not compile:

class Base {};
class Kid : private Base {};
Base instance;
class Grandkid : public Kid
{
    const Base &GetInstance  () const
    { return instance; }
};

At least my compiler (MinGW 5.3, i.e. gcc for Windows) gives

error 'class Base Base::Base' is inaccessible at {}; 
error: within this context const Base &getInstance () const

For my understanding, this does not make sense, since I do not call the Base constructor at this stage, but return a link (to the Base instance).

Please note that the error can be fixed using

const ::Base &GetInstance  () const
{ return instance; }

C++ 03 §11.2/3 (. [ C++ /)

. , .

, . - ?

+4
2

Base, Base (, , GrandKid). , , . GrandKid, Kid.

Base::Base , Base, Base. , , , , Base::Base , , :

class Foo {};

std::vector<Foo::Foo::Foo::Foo> v;

[ ]

, , Base::Base, , , (++ 11 [class.qual] 3.4.3.1/2)

+2

, Base - , Base.

:

class Base {};
class Kid : private Base {};
Base instance;
class Grandkid : public Kid
{
    const class Base &GetInstance  () const
    { return instance; }
};

- :

class Base {
public:
    struct BaseStruct { int a; };
};
class Kid : private Base {
public:
    struct KidStruct { int a; };
};
Base instance;
class Grandkid : public Kid
{
    const class Base &GetInstance  () const
    { return instance; }

    const Kid::KidStruct kidStruct;
    const Base::BaseStruct baseStruct;
};

kidStruct , , baseStruct .

0

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


All Articles