Original Post
I thought I knew this stuff, but apparently I don't... I have the following code, which doesn't compile: It gives the compile error "error C2248: 'Base::foo' : cannot access protected member declared in class 'Base'". Why is that? I thought that protected meant "This can be accessed by instances of this class, or its derived classes", but it seems to not be the case if I try to access foo() from a different instance of a derived class. This seems "wrong" to me (Although I'm sure it's not), since if I change x to a Derived, it compiles fine. I don't really see why it should make a difference, since the call to foo() is still coming from the same object type... So, what is the "correct" definition of protected?
class Base
{
protected:
void foo() {}
};
class Derived : public Base
{
public:
void Bar()
{
Base x;
x.foo();
}
};
int main(int argc, char** argv))
{
Derived a;
a.Bar();
return 0;
}