When is a base class protected member not visible in a derived class?

Discussion in 'C++' started by Andy Lomax, Jun 29, 2005.

  1. Andy Lomax

    Andy Lomax Guest

    Can anyone tell me why the code below doesn't compile?

    The code has a simple hierarchy of publically-derived classes: A -> B
    -> C. A declares a protected member 'foo'. C declares an object of
    type B, and attempts to access B.foo, which results in a compiler
    error:

    test.cpp: In member function `void C::test()':
    test.cpp:5: error: `int A::foo' is protected
    test.cpp:15: error: within this context

    Cheers -

    AL

    ----------------------------------------------------------
    #include <iostream>

    class A {
    protected:
    int foo;
    };

    class B: public A {
    };

    class C: public B {
    public:
    void test() {
    B b;
    std::cout << b.foo << std::endl;
    }
    };

    int main() {
    C c;
    c.test();
    }
    ----------------------------------------------------------
    Andy Lomax, Jun 29, 2005
    #1
    1. Advertising

  2. Re: When is a base class protected member not visible in a derivedclass?

    Andy Lomax wrote:
    > Can anyone tell me why the code below doesn't compile?


    See below.

    > The code has a simple hierarchy of publically-derived classes: A -> B
    > -> C. A declares a protected member 'foo'. C declares an object of
    > type B, and attempts to access B.foo, which results in a compiler
    > error:
    >
    > test.cpp: In member function `void C::test()':
    > test.cpp:5: error: `int A::foo' is protected
    > test.cpp:15: error: within this context


    Yes. The cause: a common misconception of what 'protected' is for.

    >
    > Cheers -
    >
    > AL
    >
    > ----------------------------------------------------------
    > #include <iostream>
    >
    > class A {
    > protected:
    > int foo;
    > };
    >
    > class B: public A {
    > };
    >
    > class C: public B {
    > public:
    > void test() {
    > B b;
    > std::cout << b.foo << std::endl;


    Inside a C object you're only allowed to access protected members of
    _your_own_ instance (through this->) or of another instance of type C.
    This should be OK:

    this->foo;
    C c;
    c.foo;

    > }
    > };
    >
    > int main() {
    > C c;
    > c.test();
    > }
    > ----------------------------------------------------------


    V
    Victor Bazarov, Jun 29, 2005
    #2
    1. Advertising

  3. Andy Lomax

    Allan Bruce Guest

    "Andy Lomax" <abuse@[127.0.0.1]> wrote in message
    news:...
    > Can anyone tell me why the code below doesn't compile?
    >
    > The code has a simple hierarchy of publically-derived classes: A -> B
    > -> C. A declares a protected member 'foo'. C declares an object of
    > type B, and attempts to access B.foo, which results in a compiler
    > error:
    >
    > test.cpp: In member function `void C::test()':
    > test.cpp:5: error: `int A::foo' is protected
    > test.cpp:15: error: within this context
    >
    > Cheers -
    >
    > AL
    >
    > ----------------------------------------------------------
    > #include <iostream>
    >
    > class A {
    > protected:
    > int foo;
    > };
    >
    > class B: public A {
    > };
    >
    > class C: public B {
    > public:
    > void test() {
    > B b;
    > std::cout << b.foo << std::endl;
    > }
    > };
    >
    > int main() {
    > C c;
    > c.test();
    > }
    > ----------------------------------------------------------


    As Victor said, protected is for internal access - if you want to have
    external access like you have then you can use the 'friend' keyword.

    Allan
    Allan Bruce, Jun 29, 2005
    #3
  4. Andy Lomax

    Andy Lomax Guest

    On Wed, 29 Jun 2005 11:08:33 -0400, Victor Bazarov
    <> wrote:

    >Andy Lomax wrote:
    >> Can anyone tell me why the code below doesn't compile?

    >
    >Yes. The cause: a common misconception of what 'protected' is for.


    Great.

    Thanks -

    AL
    Andy Lomax, Jun 29, 2005
    #4
  5. Andy Lomax

    anthonyhan Guest

    Another way.

    class C: public B {
    public:
    void test() {
    std::cout << B::foo << std::endl;
    }
    anthonyhan, Jun 30, 2005
    #5
  6. Andy Lomax

    John Carson Guest

    "anthonyhan" <> wrote in message
    news:
    > Another way.
    >
    > class C: public B {
    > public:
    > void test() {
    > std::cout << B::foo << std::endl;
    > }


    That is equivalent to

    class C: public B {
    public:
    void test() {
    std::cout << foo << std::endl;
    }

    It accesses the protected member in the object's *own* B subobject and
    doesn't address the original issue, which is accessing a protected member in
    *another* instance of B.


    --
    John Carson
    John Carson, Jun 30, 2005
    #6
    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:
    783
    Micah Cowan
    Jan 12, 2005
  3. Replies:
    1
    Views:
    383
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    371
    Victor Bazarov
    May 23, 2007
  5. blangela
    Replies:
    8
    Views:
    647
    Erik Wikström
    Sep 26, 2008
Loading...

Share This Page