Accessing parent's protected variables

前端 未结 4 2008
夕颜
夕颜 2020-12-03 17:03

I couldn\'t think of a better wording for the title, so it is a little misleading, however, I am not talking about a child accessing its variables inherited from its parent,

相关标签:
4条回答
  • 2020-12-03 17:30

    This is so easy (meaning the apparent misunderstanding of the OP, is because people aren't taking the time to read the OP).

    You simply make the child a friend of the parent's variable that you need to access.

    Or, you can make the child a friend of the parent class.

    That way any child has access to any parent's member variables, exactly the way you are expecting.

    class Child;
    
    class Parent {
      protected:
         Parent *target;
         int hp;
         friend void Child::my_func();
    }
    
    class Child : public Parent {
      public:
         void my_func();
    }
    
    void Child::my_func() {
        target->hp -= 50;
    }
    

    The downside to this is that EVERY child can have access to the variables of EVERY parent. However, you must consider that in your case, the compiler cannot know that Parent *target is the same instance as the child. Given that you named it target, I would expect that having EVERY child have access to variables of EVERY parent is what you want.

    Here's another possibility. Have everyone else use an interface to access the parent, and have only your child use the actual parent class. The result is the same though. Every child has access to every parents variables.

    You're confusing class with instance. The child has access to the same member variables of the base class that is of the same INSTANCE.

    0 讨论(0)
  • 2020-12-03 17:45

    hmm, strange nobody mentioned this so far, but you could declare Child to be a friend of Parent (maybe because your code isn't very clear about what exactly you want to do here)

    class Parent {
      friend class Child;
      protected:
         int hp;
    }
    
    class Child {
      public:
         void my_func();
         Parent *target;
    }
    

    this would allow access. alternatively you could write an accessor method that's public:

    class Parent {
    public:
      get_hp(){return hp;}
    protected:
      int hp;
    }
    
    0 讨论(0)
  • 2020-12-03 17:48

    Member functions of a particular class only have access to protected members of base classes that actually are base class subobjects of objects of their own class type (or more derived types).

    Members of one class do not have access to protected members of other instances of that base class and so are also forbidden from accessing protected members through a reference or pointer to the base class type even if at runtime that pointer or reference might be to an object that is of the type of the class whose member function is attempting the access. Access control is enforced at compile time.

    E.g.

    class X
    {
    protected:
        int z;
    };
    
    class Y : X
    {
    public:
        int f( const Y& y )
        {
            return y.z; // OK
        }
    
        int g( const X& x )
        {
            return x.z; // Error, Y::g has no access to X::z
        }
    };
    

    In your example, in the expression target->hp, the access to target is legal because you are accessing a member of the current object (which has the type of the class of which the function is a member, Child), but the access to the member hp is not legal because the type of target is not a pointer to Child, but a pointer to Parent.

    0 讨论(0)
  • 2020-12-03 17:48

    Try to change to this

     Class Child : public Parent
    
    0 讨论(0)
提交回复
热议问题