Protected member inaccessible from derived class - why

Discussion in 'C++' started by avasilev, Nov 21, 2011.

  1. avasilev

    avasilev Guest

    Hi all,
    Quick question: why doesn't this code compile:

    class Base
    {
    protected:
    int a;
    };

    class Derived: public Base
    {
    public:
    void test(Base* b)
    {
    b->a = 5;
    }
    };

    int main()
    {
    Derived d;
    Derived d1;
    d.test(&d1);
    }
    The error is that Base::a is protected in the test() method of Derived
    Thanks in advance
    Best regards
    Alex
    avasilev, Nov 21, 2011
    #1
    1. Advertising

  2. On 11/21/2011 6:10 AM, avasilev wrote:
    > Quick question: why doesn't this code compile:
    >
    > class Base
    > {
    > protected:
    > int a;
    > };
    >
    > class Derived: public Base
    > {
    > public:
    > void test(Base* b)
    > {
    > b->a = 5;
    > }
    > };
    >
    > int main()
    > {
    > Derived d;
    > Derived d1;
    > d.test(&d1);
    > }
    > The error is that Base::a is protected in the test() method of Derived


    The access to protected members is allowed only for the '*this' object,
    and not for any other object. IOW, in 'Derived::test' you should be
    able to access 'this->a', but not 'anyotherBase.a'. That's just the
    rule of the language.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 21, 2011
    #2
    1. Advertising

  3. avasilev

    avasilev Guest

    On Nov 21, 2:51 pm, Victor Bazarov <> wrote:
    > On 11/21/2011 6:10 AM, avasilev wrote:
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > > Quick question: why doesn't this code compile:

    >
    > > class Base
    > > {
    > > protected:
    > >   int a;
    > > };

    >
    > > class Derived: public Base
    > > {
    > > public:
    > >    void test(Base* b)
    > >    {
    > >      b->a = 5;
    > >    }
    > > };

    >
    > > int main()
    > > {
    > >    Derived d;
    > >    Derived d1;
    > >    d.test(&d1);
    > > }
    > > The error is that Base::a is protected in the test() method of Derived

    >
    > The access to protected members is allowed only for the '*this' object,
    > and not for any other object.  IOW, in 'Derived::test' you should be
    > able to access 'this->a', but not 'anyotherBase.a'.  That's just the
    > rule of the language.
    >
    > V
    > --
    > I do not respond to top-posted replies, please don't ask


    Thanks for the response, Victor

    I thought about this, but I think I remember having written operator
    methods, that contain something like:


    MyClass& operator=(MyClass& other)
    {
    mMember = other.mMember;
    ...
    }

    where mMember was internal protected stuff. IS there any exception
    for operators, or I am just mistaken?

    Thanks
    avasilev, Nov 21, 2011
    #3
  4. On 21.11.2011 14:10, avasilev wrote:
    >>> void test(Base* b)

    >
    > MyClass& operator=(MyClass& other)
    > {
    > mMember = other.mMember;
    > ...
    > }
    >
    > where mMember was internal protected stuff. IS there any exception
    > for operators, or I am just mistaken?


    Can you see any obvious difference above?

    Cheers & hth.,

    - Alf
    Alf P. Steinbach, Nov 21, 2011
    #4
    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. Shelly Adhikari
    Replies:
    3
    Views:
    353
    red floyd
    Sep 10, 2003
  2. Siemel Naran
    Replies:
    4
    Views:
    784
    Micah Cowan
    Jan 12, 2005
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,739
    Smokey Grindel
    Dec 2, 2006
  4. Andy Lomax
    Replies:
    5
    Views:
    449
    John Carson
    Jun 30, 2005
  5. blangela
    Replies:
    8
    Views:
    647
    Erik Wikström
    Sep 26, 2008
Loading...

Share This Page