How can I access the data in a parent’s class, which is protected, when passed into a derived class.
class parent
{
protected:
int a;
};
class child : public parent
{
void addOne(parent * &);
};
void child::addOne(parent * & parentClass)
{
parentClass->a += 1;
}
int main()
{
parent a;
child b;
parent* ap = &a;
b.addOne(ap);
}
You cannot access protected data via a pointer/reference to the base class. This is to prevent you from breaking the invariants that other derived classes may have on that data.