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 HeirsHeir
they 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++ /)
. , .
, . - ?