Error on C++: "error: int aaa::bbb is protected within this context"

Viewed 2621

After years of programming in C, I'm doing my first steps in C++.

I'm trying to grasp the "protected" concept, and there is a lot of material on the web that explains what a protected variable is, and what they are used for. However, when trying to code a super-basic example, just to get my hands dirty with C++, I bumped into an error:

error: 'int parent::protected1' is protected within this context

So, a little help will be appreciated.

class parent {
public:
    int getProtected() { return protected1; }
protected:
    int protected1;
};

class child: public parent { };

int main()
{
    child ch;

    cout << ch.protected1 << endl;    // error: 'int parent::protected1' is protected within this context
    cout << ch.getProtected() << endl;   // OK
    return 0;
}

Everywhere it is said that protected variables are only accessible within an inheritance hierarchy. If that's the case, I'm trying to understand - what am I doing wrong here?

The concept of "protected variable" hasn't really sunk in. The private is well understood, as a private variable belongs to the child instance and therefore can be accessed only by child methods. However, if a child can access a protected variable of the parent, does it mean a parent must be instantiated before a child can access this protected variable?

4 Answers

A protected member variable can only be accessed via member functions of the parent or child class, as your example shows. Thus:

ch.protected1

does not compile since you are trying to access the data member from outside the class.

1st question: Everywhere it is said that Protected variables are only accessible within an inheritance hierarchy. If that's the case, I'm trying to understand - what am I doing wrong here.

You're trying to access a protected member from main and main is outside the inheritance hierarchy. Just as private hides part of the implementation of a class from code outside that class, protected hides part of the implementation of a class from code outside that hierarchy. Since main is both outside the class and outside the hierarchy, both protected and private hide from it.

2nd question: The concept of "protected variable" hasn't really sunk in. The "private" is well understood, as a private variable belongs to the child instance and therefore can be accessed only by child methods. However, if a child can access a protected variable of the parent, does it mean a parent must be instantiated before a child can access this protected variable?

A parent must be instantiated before a child can even be instantiated. Children are instances of their parents.

If you have class car : public vehicle, then every car is also a vehicle. If you have an instance of the class car, you necessarily also have an instance of vehicle.

protected means that your class child has access to the parent (from which it publicly inherits) class' protected1 field. If instead protected was private, child wouldn't be able to access this variable.

Your main() function isn't in a derivative class of parent - therefore it cannot access protected fields.

You are trying to access directly the class member protected1. This is possible only if the member is public.

Your class child can still access that member, so you could try:

class parent {
public:
    int getProtected() { return protected1; }
protected:
    int protected1;
};

class child: public parent { 
public:
  int getProtectedFromChild() { return protected1; }
};

int main()
{
    child ch;

    cout << ch.getProtected() << endl;   // OK
    cout << ch.getProtectedFromChild() << endl;   // This should work
    return 0;
}
Related