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,
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.
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;
}
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
.
Try to change to this
Class Child : public Parent