protected access

Discussion in 'C++' started by Kevin Saff, Jul 25, 2003.

  1. Kevin Saff

    Kevin Saff Guest

    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?
    Kevin Saff, Jul 25, 2003
    #1
    1. Advertising

  2. "Kevin Saff" <> 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
    Victor Bazarov, Jul 25, 2003
    #2
    1. Advertising

  3. "Victor Bazarov" <> wrote in message
    news:...
    > "Kevin Saff" <> 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
    Michael Furman, Jul 26, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Klemt
    Replies:
    2
    Views:
    556
    Andreas Klemt
    Jul 5, 2003
  2. Paul Lynch
    Replies:
    1
    Views:
    408
    www.MSmobiles.com
    Jul 15, 2004
  3. =?Utf-8?B?TmF2ZWVu?=
    Replies:
    2
    Views:
    9,548
    =?Utf-8?B?TmF2ZWVu?=
    Jun 4, 2005
  4. Kenneth McDonald
    Replies:
    0
    Views:
    474
    Kenneth McDonald
    Feb 7, 2009
  5. Ken McDonald
    Replies:
    2
    Views:
    843
    Kenneth McDonald
    Feb 11, 2009
Loading...

Share This Page