【发布时间】:2012-03-28 13:11:21
【问题描述】:
$ cat inheritance.cpp
#include <iostream>
using namespace std;
class A { };
class B : private A { };
int main() {
A* ab = new B;
}
$
$ g++ inheritance.cpp
inheritance.cpp: In function 'int main()':
inheritance.cpp:9: error: 'A' is an inaccessible base of 'B'
$
我只是不明白这个错误。
据我所知,正如this tutorial 所确认的那样,private 继承只会改变class B 的成员对外部世界的可见性。
我认为私有说明符不仅仅是改变class B 成员的可见性。
- 为什么在继承时会出现这个错误,这是什么意思?
- 在 C++ 中允许这种类型的代码基本上有什么问题?看起来完全无害。
【问题讨论】:
标签: c++ inheritance