C++ 如何让派生类访问私有成员数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18944451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:21:47  来源:igfitidea点击:

How to make a derived class access the private member data?

c++

提问by Rahul Chitta

I'm stuck with a c++ problem. I have a base class that has a self referential object pointer inside the private visibility region of the class. I have a constructor in the base class that initializes these two pointers. Now I have my derived class whose access specifier is private(I want to make the public member functions of my base class private). Now through the member functions of my derived class I want to create an object pointer which can point to the private data of the base class, that is ,those self referential object pointers. My code is:

我被一个 C++ 问题困住了。我有一个基类,它在类的私有可见区域内有一个自引用对象指针。我在基类中有一个构造函数来初始化这两个指针。现在我有我的派生类,其访问说明符是私有的(我想将基类的公共成员函数设为私有)。现在通过我的派生类的成员函数,我想创建一个对象指针,它可以指向基类的私有数据,即那些自引用对象指针。我的代码是:

class base{
private:
     base *ptr1;
     int data;
public:
     base(){}
     base(int d) { data=d }
};

class derived:private base{
public:
     void member()
};

void derived::member()
{
base *temp=new base(val); //val is some integer
temp->ptr1=NULL; //I can't make this happen. To do this I had to declare all the
                 //private members of the base class public. 
}

回答by Rahul Tripathi

Derived class can not access the private members of it's base class. No type of inheritance allows access to private members.

派生类无法访问其基类的私有成员。没有任何类型的继承允许访问私有成员。

However if you use frienddeclaration you can do that.

但是,如果您使用friend声明,则可以这样做。

回答by cpp

There is no other way to access other class's private data then friendship. What you can do with inheritance, however, is to access protected data of the base class. But it doesn't mean you can access protected data of another object of the base type. You can only access protected data of the base partof the derived class:

除了友谊之外,没有其他方法可以访问其他班级的私人数据。但是,您可以使用继承来访问基类的受保护数据。但这并不意味着您可以访问基类型的另一个对象的受保护数据。您只能访问派生类的基本部分的受保护数据:

class base{
protected:  //protected instead of private
     base *ptr1;
     int data;
public:
     base(){}
     base(int d) { data=d; }
};

class derived:private base{
public:
     void member();
};

void derived::member()
{
    base *temp=new base(3); 
    //temp->ptr1 = 0; //you need friendship to access ptr1 in temp

    this->ptr1 = 0; // but you can access base::ptr1 while it is protected
}

int main(){}

回答by rahul kumar

try to give protected as the access specifier in base class and inherit the base class in private mode.....but for further using member functions of base class u may need few short inline functions as they will also be converted to private

尝试将 protected 作为基类中的访问说明符并在私有模式下继承基类.....但是为了进一步使用基类的成员函数,您可能需要一些简短的内联函数,因为它们也将被转换为私有

回答by Parag

Well I think, you were trying to achieve a result like this!! This does not report any compiler error or such. Good luck!!!

好吧,我想,你是想达到这样的结果!!这不会报告任何编译器错误等。祝你好运!!!

class base{
private:
     base *ptr1;
     int data;
public:
     base(){}
     base(int d) { data=d; }
     base* getPtr();   //getter to get access to base pointer
     void setPtr(base* val); // setter to set value of the pointer variable
};

base* base::getPtr()
{
    return ptr1;
}

void base::setPtr(base* val)
{
    ptr1 = val;
}

class derived:private base{
private:
    base* getPtr();
    void setPtr(base* val);
public:
     void member();

};

base* derived::getPtr()
{
    return base::getPtr(); //derived version just invokes the base class version

}

void derived::setPtr(base* val)
{
    base::setPtr(val);     //derived version just invokes the base class version

}
void derived::member()
{
    base *temp=new base(5); //put in a random number here instead of val
    temp -> setPtr(nullptr);
}

回答by Edward Severinsen

I disagree with some of the other answers claiming the only way to access the private member is by making it a friend.

我不同意其他一些答案,声称访问私人成员的唯一方法是将其设为friend.

You can directly access the private member via its address in memory. If you're comfortable with it that is. You could have a function in the base class that returns the address of the private member and then use some wrapping function in the derived class to retrieve, dereference and set the private member.

您可以通过其在内存中的地址直接访问私有成员。如果您对此感到满意,那就是。您可以在基类中有一个函数返回私有成员的地址,然后在派生类中使用一些包装函数来检索、取消引用和设置私有成员。