无法使用基指针访问受保护的基类成员

时间:2018-02-01 19:32:53

标签: c++ protected

我有一个拥有私人,受保护和公共成员的基类。此基类是公开派生的。

class base
{
    int private_c;
protected:
    int protected_c;
public:
    int public_c;

    base()
    {
        private_c = protected_c = public_c = 0;
    }

    base(int private_c, int protected_c, int public_c)
    {
        this->private_c = private_c;
        this->protected_c = protected_c;
        this->public_c = public_c;
    }

    int get_privateC()
    {
        return(private_c);
    }

    int get_publicC()
    {
        return(public_c);
    }

    int get_protectedC()
    {
        return(protected_c);
    }

    virtual void func3(base *bp)
    {
        cout << __FUNCTION__ "(): " << "private_c = " << this->get_privateC() << ", protected_c = " << protected_c << ", public_c = " << public_c << "\n";
        cout << __FUNCTION__ "(): " << "private_c = " << bp->get_privateC() << ", protected_c = " << bp->protected_c << ", public_c = " << bp->public_c << "\n";
    }
};

class derived: public base
{
    int dr_c;
public:
    derived():base(43, 73, 93)
    {

    }

    derived(int c, int d, int e):base(c, d, e)
    {

    }

    void func1(derived dp)
    {
        cout << __FUNCTION__ "(): " << "private_c = " << this->get_privateC() << ", protected_c = " << this->protected_c << ", public_c = " << this->public_c << "\n";
        cout << __FUNCTION__ "(): " << "private_c = " << dp.get_privateC() << ", protected_c = " << dp.protected_c << ", public_c = " << dp.public_c << "\n\n";
    }

    void func2(derived *dp)
    {
        cout << __FUNCTION__ "(): " << "private_c = " << this->get_privateC() << ", protected_c = " << this->protected_c << ", public_c = " << this->public_c << "\n";
        cout << __FUNCTION__ "(): " << "private_c = " << dp->get_privateC() << ", protected_c = " << dp->protected_c << ", public_c = " << dp->public_c << "\n\n";
    }

    void func3(base *bp)
    {
        cout << __FUNCTION__ "(): " << "private_c = " << this->get_privateC() << ", protected_c = " << this->protected_c << ", public_c = " << this->public_c << "\n";
//      cout << __FUNCTION__ "(): " << "private_c = " << bp->get_privateC() << ", protected_c = " << bp->protected_c << ", public_c = " << bp->public_c << "\n";
//      cout << __FUNCTION__ "(): " << "private_c = " << bp->get_privateC() << ", protected_c = " << bp->get_protectedC() << ", public_c = " << bp->public_c << "\n";
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    cout << __FUNCTION__ "()\n";

    derived dobj1(2,4,6), dobj2(3,5,7);

    derived *dptr1 = &dobj1;
    derived *dptr2 = &dobj2;

    base *bptr1 = &dobj1;
    base *bptr2 = &dobj2;

    dobj1.func1(dobj2);
    dptr1->func2(dptr2);
    bptr1->func3(bptr2);

    return(0);
}

这将输出为:

wmain()
derived::func1(): private_c = 2, protected_c = 4, public_c = 6
derived::func1(): private_c = 3, protected_c = 5, public_c = 7

derived::func2(): private_c = 2, protected_c = 4, public_c = 6
derived::func2(): private_c = 3, protected_c = 5, public_c = 7

derived::func3(): private_c = 2, protected_c = 4, public_c = 6
Press any key to continue . . .

我必须在派生函数中访问两个派生类对象的受保护成员。为此,我尝试了三个函数func1()func2()func3()

如果我使用函数func1()将派生对象传递给它,我可以访问受保护的基类成员OF THE PARAMETER。 在函数func2()中,我传递了派生类指针,并且能够访问受保护的成员参数。

但是当我使用基类指针作为函数func3(中的参数)并尝试访问(通过取消注释cout中的第二个func3())参数的受保护成员然后它给我编译错误:

error C2248: 'base::protected_c' : cannot access protected member declared in class 'base'

我必须使用(如cout的{​​{1}}声明中所见)公共函数func3()来访问受保护的成员。

为什么我无法使用基指针直接在派生类中访问受保护的成员?

0 个答案:

没有答案