protected access

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?
 
V

Victor Bazarov

Kevin Saff said:
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();
}
};


No, you can only access 'speak' in the same object as '*this'.
You cannot access 'speak' in another object because it may not
be your subobject. Imagine

class DD : public A, public B { ...whatever... };

D d;
DD dd;

d.listen(dd);

what would happen? You'd try to access a part of object from
a different hierarchy.
// error C2248: 'speak' : cannot access protected member declared in class
'B'

Is this correct?
Yes.

So, protected access is granted only to the derived
object, rather than the derived class?
Yes.

Can I do something like this without
relying on public access?

Describe the problem you're trying to solve.

Victor
 
M

Michael Furman

Victor Bazarov said:
Kevin Saff said:
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();
}
};


No, you can only access 'speak' in the same object as '*this'.
You cannot access 'speak' in another object because it may not
be your subobject. Imagine

That is not true. Correct sentence would be:
"you can only access 'speak' in the object of the same class as '*this'".
The rule is exactly the same as for "private" - you can replace "protected"
by "private" in the example and try.

To make it compilable you have to change function 'listen' to accept
reference to the class "D" (or some other class derived from "D"):

void listen(D& other) {
std::cout << "It says: ";
other.speak();
}

[...]
So, protected access is granted only to the derived
object, rather than the derived class?

Yes.

No. It is granted to derived class. But you can access it only in the
objects
of the derived class - it in the objects of the base class.

Michael Furman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top