Inheritance: 'A' is an inaccessible base of 'B'

后端 未结 6 1329
忘掉有多难
忘掉有多难 2020-11-28 21:51
$ cat inheritance.cpp 
#include 

using namespace std;

class A { };
class B : private A { };

int main() {
    A* ab = new B;
}
$
$ g++ inheritance.         


        
6条回答
  •  渐次进展
    2020-11-28 22:18

    private inheritance should only change how the members of class B are visible to the outside world

    It does. And if

    A* p = new B;
    

    were allowed, then the inherited members of any B could be accessed from the outside world, just by making a A*. Since they're privately inherited, that access is illegal, and so is the upcast.

提交回复
热议问题