Why does having a declaration of a private base class render a type name inaccessible?

﹥>﹥吖頭↗ 提交于 2020-04-06 02:18:48

问题


I am surprised that in the following example declaring Middle's base class private makes that name unavailable as a type in a subsequent derivation.

class Base {
public:
  Base(Base const& b) : i(b.i) {}

  int i;
};

class Middle : private Base {            //<<<<<<<<<<<
public:
  Middle(Base const* p) : Base(*p) {}
};

class Upper : public Middle {
public:
  Upper(Base const* p) : Middle(p) {}    //<<<<<<<<<<<
};

Compiling thusly with g++ (Debian 6.3.0-18+deb9u1) 6.3.0 20170516...

g++ -std=c++11 privateBase.cpp

I get the following diagnostics:

privateBase.cpp:15:9: error: ‘class Base Base::Base’ is inaccessible within this context
   Upper(Base const* p) : Middle(p) {}
         ^~~~
privateBase.cpp:1:12: note: declared here
 class Base {
            ^

Clearly at the point that Base was used as Middle's base class its name was available as a type. I can understand that when Base is used to denote base class storage that should be private. But having a declaration of a private base class render a type name inaccessible seems, at the very least, unexpected.


回答1:


This is intended; see core issue 175, which even added an example illustrating this in [class.access.spec]p5:

[ Note: In a derived class, the lookup of a base class name will find the injected-class-name instead of the name of the base class in the scope in which it was declared. The injected-class-name might be less accessible than the name of the base class in the scope in which it was declared. — end note ] [ Example:

class A { };
class B : private A { };
class C : public B {
  A* p;             // error: injected-class-name A is inaccessible
  ::A* q;           // OK
};

— end example ]


This falls out of the interaction between class name injection (for rationale, see Why is there an injected class name?) and the fact that in C++ access control applies after name lookup, not before.



来源:https://stackoverflow.com/questions/53949504/why-does-having-a-declaration-of-a-private-base-class-render-a-type-name-inacces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!