c ++将基类分配给Derived

时间:2015-01-01 16:20:42

标签: c++

为什么这不会编译?我错过了什么?

class Base
{
    public:
        Base() : data(0) { }
    protected:
        int data;
};

class Derived : public Base
{
    public:
        Derived() : Base() { }
        Derived& operator=(const Base& _base)
        {
            data = _base.data; // protected member Base::data is not accessible through a "Base" pointer or object.
        }
};

为什么会这样,因为数据成员具有受保护的可见性(以便派生类可以访问它)。

0 个答案:

没有答案