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

前端 未结 5 875
忘掉有多难
忘掉有多难 2021-02-06 03:42

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

5条回答
  •  时光说笑
    2021-02-06 03:59

    I disagree with some of the other answers claiming the only way to access the private member is by making it a 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.

提交回复
热议问题