protected access

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kevin Saff

    protected access

    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?



  • Victor Bazarov

    #2
    Re: protected access

    "Kevin Saff" <google.com@kev in.saff.net> wrote...[color=blue]
    > 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[/color]
    and[color=blue]
    > 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[/color]
    following[color=blue]
    > 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();
    > }
    > };[/color]


    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.
    [color=blue]
    >
    > // error C2248: 'speak' : cannot access protected member declared in class
    > 'B'
    >
    > Is this correct?[/color]

    Yes.
    [color=blue]
    > So, protected access is granted only to the derived
    > object, rather than the derived class?[/color]

    Yes.
    [color=blue]
    > Can I do something like this without
    > relying on public access?[/color]

    Describe the problem you're trying to solve.

    Victor


    Comment

    • Michael Furman

      #3
      Re: protected access


      "Victor Bazarov" <v.Abazarov@att Abi.com> wrote in message
      news:vi36a0tk77 bi88@corp.super news.com...[color=blue]
      > "Kevin Saff" <google.com@kev in.saff.net> wrote...[color=green]
      > > 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[/color][/color]
      functions[color=blue][color=green]
      > > and friends of the class in which it is declared and by member functions[/color]
      > and[color=green]
      > > friends of classes derived from this class.
      > >
      > > Since private access cares about the calling class rather than the[/color][/color]
      calling[color=blue][color=green]
      > > object, I assumed the same was true for protected access, but the[/color]
      > following[color=green]
      > > 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();
      > > }
      > > };[/color]
      >
      >
      > 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[/color]

      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();
      }

      [color=blue]
      > [...][color=green]
      > > So, protected access is granted only to the derived
      > > object, rather than the derived class?[/color]
      >
      > Yes.[/color]

      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


      Comment

      Working...