There is no other way to access other private class data, and then friendship. However, you can make access to the protected data of the base class with inheritance. But this does not mean that you can access the protected data of another object of the base type. You can only access the protected data of the base part of 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(){}
source share