I am trying to access data of the superclass within the subclass, but i need to do this by passing a different pointer (i.e. not using *this
) into the function. The test code would look like the following:
class Superclass
{
protected:
int data;
};
class Subclass : public Superclass
{
public:
void foo(Superclass *p)
{
p->data = 5;
}
};
int main()
{
Superclass sup;
Subclass sub;
sub.foo(&sup);
return 0;
}
I would expect it to work, but this produces an error:
error: ‘int Superclass::data’ is protected within this context
Why is this protected, when i should be able to see the data of the superclass?