源码
// 14Inhe_private.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream.h"
class Base
{
public:
void fun1(){}
};
class Derived : Base//私有继承
{
};
int main(int argc, char* argv[])
{
Derived d;
d.fun1();//这句话正确吗?私有继承有什么用呢?
return 0;
}
这句话正确吗?私有继承有什么用呢?
不正确。
第一个规则:和公有继承相反,如果两个类之间的继承关系为私有,编译器一般不会将派生类对象转换成基类对象。
第二个规则: 从私有基类继承而来的成员都成为了派生类的私有成员,即使它们在基类中是保护或公有成员。
通常你不会想访问其他类的内部,而私有继承给你这样的一些的特权(和责任)