Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing parent's protected variables

Tags:

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, which is easy enough.

What I am talking about is this:

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

However, if I try to compile this, it will complain about 'hp' being "private in this context". The problem is that the child is not attempting to access its own parent's variables, but some other class', which may or may not be a Child itself.

An object can access all the variables and methods (public, protected, or private) of another object (two separate instances in memory) that is of the same class, so I thought that it would work with this as well, as it inherits from the class whose variables it's attempting to access, but it seems I was incorrect in assuming so.

Any tips?

P.S. Not to be rude or anything, but I know that I can just create get() and set() methods, but I was hoping for a cleaner way.

like image 309
Infiltrator Avatar asked Jan 28 '11 14:01

Infiltrator


People also ask

Can you access protected variables?

Protected Access Modifier - Protected Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.

Can child class access protected variables?

Yes, a derived class can access a protected variable in a base class via both 'super' and another reference to the base class.

Can an inherited class access protected members?

The protected members are inherited by the child classes and can access them as its own members. But we can't access these members using the reference of the parent class. We can access protected members only by using child class reference.

Who can access protected member C++?

Protected members that are also declared as static are accessible to any friend or member function of a derived class. Protected members that are not declared as static are accessible to friends and member functions in a derived class only through a pointer to, reference to, or object of the derived class.


2 Answers

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.

like image 81
CB Bailey Avatar answered Sep 28 '22 04:09

CB Bailey


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.

like image 34
Lee Louviere Avatar answered Sep 28 '22 03:09

Lee Louviere