且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何访问派生类中的受保护成员?

更新时间:2023-02-14 22:43:26

I think that the thing you are trying to do should looks like this:

#include <iostream>
using namespace std;

class X
{
    private:
        int var;
protected:
    virtual void fun () 
    {
        var = 10;
        cout << "\nFrom X" << var; 
    }
};

class Y : public X
{
private:
    int var;
public:
    virtual void fun () 
    {
        var = 20;
        cout << "\nFrom Y" << var;
    }

    void call ()
    {
        fun ();


        X::fun ();
    }
};

That way you can invoke hiden member from your base class. Otherwise you have to add friend X as it was pointed in other post.

上一篇 : :通过指向其基类的指针删除派生对象下一篇 : 自动将应用程序注册到Azure AD for SSO

相关阅读

技术问答最新文章