K
Kevin Saff
Apparently I'm missing something. Stroustrup (15.3) says of protected
access:
If [a member] is protected, its name can be used only by member functions
and friends of the class in which it is declared and by member functions and
friends of classes derived from this class.
Since private access cares about the calling class rather than the calling
object, I assumed the same was true for protected access, but the following
code fails in MSVC6:
class B
{
protected:
virtual void speak() {
std::cout << "Howdy, I'm B" << std::endl;
}
};
class D : public B
{
public:
void listen(B& other) {
std::cout << "It says: ";
other.speak();
}
};
// error C2248: 'speak' : cannot access protected member declared in class
'B'
Is this correct? So, protected access is granted only to the derived
object, rather than the derived class? Can I do something like this without
relying on public access?
access:
If [a member] is protected, its name can be used only by member functions
and friends of the class in which it is declared and by member functions and
friends of classes derived from this class.
Since private access cares about the calling class rather than the calling
object, I assumed the same was true for protected access, but the following
code fails in MSVC6:
class B
{
protected:
virtual void speak() {
std::cout << "Howdy, I'm B" << std::endl;
}
};
class D : public B
{
public:
void listen(B& other) {
std::cout << "It says: ";
other.speak();
}
};
// error C2248: 'speak' : cannot access protected member declared in class
'B'
Is this correct? So, protected access is granted only to the derived
object, rather than the derived class? Can I do something like this without
relying on public access?