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

 
        
简介:B PRIVATE/protected 继承A,那么 A 的指针不能指向B
分析:private表示派生类是更加严格的基类或者在某方面继承了基类。
它不遵守 Liskov substitution principle
$ 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'
$

I just do not understand this error.

As I understand, and as this tutorial confirms, private inheritance should only change how the members of class B are visible to the outside world.

I think the private specifier is doing more than just change visibility of class B members here.

  • What do I get this error and what does it mean?
  • Basically what is wrong with allowing this type of code in C++? Looks totally harmless.
share | improve this question

 
feedback

5 Answers

up vote 6 down vote accepted

By making the inheritance private, you're basically saying that even the fact that B inherits from A (at all) is private -- not accessible/visible to the outside world.

Without getting into a long-winded discussion of what would happen if it was allowed, the simple fact is that it's not allowed. If you want to use a pointer to base to refer to an object of derived type, then you're pretty much stuck with using public inheritance.

Edit: Since somebody went to the trouble of sending an email to ask for more information about what could happen if this was allowed, I guess I'll elaborate a little on it.

The basic problem is that private inheritance is not necessarily intended to follow the Liskov substitution principle. Public inheritance asserts that a derived object can be substituted for an object of the base class, and proper semantics will still result. Private inheritance does not assert that though. The usual description of the relationship implied by private inheritance is "is implemented in terms of".

Public inheritance means a derived class maintains all the capabilities of the base class and potentially adds more besides. Private inheritance often means more or less the opposite: that the derived class uses a general base class to implement something with a more restricted interface.

Just for example, let's assume for the moment that the containers in the C++ standard library were implemented using inheritance rather than templates. In the current system, std::deque andstd::vector are containers, and std::stack is a container adapter that provides a more restricted interface. Since it is based on templates, you can use std::stack as an adapter for eitherstd::deque or std::vector.

If we wanted to provide essentially the same with inheritance, we would probably use private inheritance, so std::stack would be something like:

class stack : private vector {
    // ...
};

In this case, we definitely do not want the user to be able to manipulate our stack as if it were avector. Doing so could (and likely would) violate the expectations of a stack (e.g., the user could insert/remove items in the middle, rather than a purely stack-like fashion as intended). We're basically using vector as a convenient way to implement our stack, but if (for example) we changed the implementation for stack stand alone (with no dependence on a base class) or re-implement it in terms of std::deque, we do not want that to affect any client code -- to the client code, this is supposed to be just a stack, not some specialized variety of vector (or deque).

原址:http://stackoverflow.com/questions/9661936/inheritance-a-is-an-inaccessible-base-of-b

转载于:https://www.cnblogs.com/catkins/archive/2012/09/24/5270759.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值