Accessing protected members in a derived class

前端 未结 8 2019
礼貌的吻别
礼貌的吻别 2020-11-22 11:30

I ran into an error yesterday and, while it\'s easy to get around, I wanted to make sure that I\'m understanding C++ right.

I have a base class with a protected memb

相关标签:
8条回答
  • 2020-11-22 11:57

    protected members can be accessed:

    • through this pointer
    • or to the same type protected members even if declared in base
    • or from friend classes, functions

    To solve your case you can use one of last two options.

    Accept Derived in Derived::DoSomething or declare Derived friend to Base:

    class Derived;
    
    class Base
    {
      friend class Derived;
      protected:
        int b;
      public:
        void DoSomething(const Base& that)
        {
          b+=that.b;
        }
    };
    
    class Derived : public Base
    {
      protected:
        int d;
      public:
        void DoSomething(const Base& that)
        {
          b+=that.b;
          d=0;
        }
    };
    

    You may also consider public getters in some cases.

    0 讨论(0)
  • 2020-11-22 11:57

    You have access to the protected members of Derived, but not those of Base (even if the only reason it's a protected member of Derived is because it's inherited from Base)

    0 讨论(0)
  • 2020-11-22 11:57
    class Derived : public Base
    {
      protected:
        int d;
      public:
        void DoSomething()
        {
          b+=this->b;
          d=0;
        }
    };
    
    //this will work
    
    0 讨论(0)
  • 2020-11-22 11:59

    Following the hack for stl I wrote a small code which seems to solve the problem of accessing the protected members in derived class

    #include <iostream>
    
    class B
    {
    protected:
        int a;
    public:
        void dosmth()
        {
            a = 4;
        }
    
        void print() {std::cout<<"a="<<a<<std::endl;}
    };
    
    class D: private B
    {
    public:
        void dosmth(B &b)
        {
            b.*&D::a = 5;
        }
    };
    
    int main(int argc, const char * argv[]) {
    
        B b;
        D d;
        b.dosmth();
        b.print();
        d.dosmth(b);
        b.print();
    
        return 0;
    }
    
    

    Prints

    a=4
    a=5
    
    0 讨论(0)
  • 2020-11-22 12:02

    You can try with static_cast< const Derived*>(pBase)->Base::protected_member ...

    class Base
    {
      protected:
        int b;
    
      public:
        ...
    };
    
    class Derived : public Base
    {
      protected:
        int d;
    
      public:
        void DoSomething(const Base& that)
        {
          b += static_cast<const Derived*>(&that)->Base::b;
          d=0;
        }
        void DoSomething(const Base* that)
        {
          b += static_cast<const Derived*>(that)->Base::b;
          d=0;
        }
    };
    
    0 讨论(0)
  • 2020-11-22 12:09

    Use this pointer to access protected members

    class Derived : public Base
    {
      protected:
        int d;
      public:
        void DoSomething(const Base& that)
        {
          this->b+=that.b;
          d=0;
        }
    };
    
    0 讨论(0)
提交回复
热议问题