Let's say I have this code:
class foo{
protected:
int a;
};
class bar : public foo {
public:
void copy_a_from_foo(foo& o){
a = o.a; // Error
}
void copy_a_from_bar(bar& o){
a = o.a; // OK
}
};
int main(){
bar x;
foo y;
bar z;
x.copy_a_from_foo(y);
x.copy_a_from_bar(z);
}
here class bar
has no problems accessing the protected member a
from another instance of the same class, but when I try to do the same with an instance of the base class foo
, the compiler gives me an error saying that a
is protected. What does the standard has to say about this?
error is
prog.cpp: In member function 'void bar::copy_a_from_foo(foo&)':
prog.cpp:3:7: error: 'int foo::a' is protected
int a;
^
prog.cpp:9:11: error: within this context
a = o.a;
P.S.: I had a look at this question, but it's not quite the same: I'm trying to access the protected member from within the derived class.