Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > protected access

Reply
Thread Tools

protected access

 
 
Kevin Saff
Guest
Posts: n/a
 
      07-25-2003
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?



 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-25-2003
"Kevin Saff" <(E-Mail Removed)> wrote...
> 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


 
Reply With Quote
 
 
 
 
Michael Furman
Guest
Posts: n/a
 
      07-25-2003

"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Kevin Saff" <(E-Mail Removed)> wrote...
> > 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
access to MS Access password-protected database? Ken McDonald Python 2 02-11-2009 08:24 PM
Possible to access MS Access 2007 password-protected database fromPython? Kenneth McDonald Python 0 02-07-2009 12:58 AM
connection string to connect ms-access with password protected =?Utf-8?B?TmF2ZWVu?= ASP .Net 2 06-04-2005 03:13 PM
password protected access to files hosted by IIS Paul Lynch ASP .Net 1 07-14-2004 11:01 PM
Difference between "Protected WithEvents myClassName" And "Protected myClassName" ? Andreas Klemt ASP .Net 2 07-05-2003 12:51 AM



Advertisments