相关疑难解决方法(0)

私有,公共和受保护继承之间的区别

是什么区别public,private以及protected用C++继承?我在SO上发现的所有问题都涉及具体案例.

c++ inheritance encapsulation c++-faq access-specifier

952
推荐指数
14
解决办法
65万
查看次数

私人继承

我完全不懂这个:

class Base
{
    public:
    Base()
    {
        cout<<"Base" << endl;
    }

    virtual void call()
    {
        cout<<"Base call" << endl; 
    }
};

class Derived: private Base
{
    public:      
    Derived()
    {
        cout<<"Derived" << endl;
    } 
};

int main(void)
{
    Base *bPtr = new Derived(); // This is not allowed
}
Run Code Online (Sandbox Code Playgroud)

是因为有人可能使用bPtr调用call(),这实际上是在派生对象上完成的?或者还有其他原因吗?

c++ inheritance

7
推荐指数
3
解决办法
4543
查看次数