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

前端 未结 5 876
忘掉有多难
忘掉有多难 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.

    0 讨论(0)
  • 2021-02-06 04:04

    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 friend declaration you can do that.

    0 讨论(0)
  • 2021-02-06 04:05

    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);
    }
    
    0 讨论(0)
  • 2021-02-06 04:11

    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 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(){}
    
    0 讨论(0)
  • 2021-02-06 04:11

    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

    0 讨论(0)
提交回复
热议问题