Access to protected base member

Discussion in 'C++' started by Andy Ward, Jun 17, 2004.

  1. Andy Ward

    Andy Ward Guest

    Given the following code:
    class A
    {
    protected:
    int pro;
    };

    class B : public A
    {
    public:
    void foo()
    {
    A * a;
    a->pro;

    B * b;
    b->pro2;
    }
    protected:
    int pro2;
    };

    The line b->pro2 compiles fine, however the line a->pro does not compile.
    Could someone please explain to me why.
    Thanks.
    Andy Ward, Jun 17, 2004
    #1
    1. Advertising

  2. Andy Ward

    David Harmon Guest

    On Thu, 17 Jun 2004 12:11:48 +1200 in comp.lang.c++, "Andy Ward"
    <> wrote,
    >The line b->pro2 compiles fine, however the line a->pro does not compile.
    >Could someone please explain to me why.


    Pointer b is promised to point to a B instance, therefore it is
    legitimate for class B members to access protected members.

    Pointer a can point to A and other derived classes that are not B
    instances, therefore it would be wrong for B to have access.
    David Harmon, Jun 17, 2004
    #2
    1. Advertising

  3. Andy Ward

    Andy Ward Guest

    "David Harmon" <> wrote in message
    news:...
    > On Thu, 17 Jun 2004 12:11:48 +1200 in comp.lang.c++, "Andy Ward"
    > <> wrote,
    > >The line b->pro2 compiles fine, however the line a->pro does not compile.
    > >Could someone please explain to me why.

    >
    > Pointer b is promised to point to a B instance, therefore it is
    > legitimate for class B members to access protected members.
    >
    > Pointer a can point to A and other derived classes that are not B
    > instances, therefore it would be wrong for B to have access.
    >


    Of course, thank you.
    Andy Ward, Jun 17, 2004
    #3
  4. Andy Ward

    Hunter Hou Guest

    David Harmon wrote:

    >
    > Pointer a can point to A and other derived classes that are not B
    > instances, therefore it would be wrong for B to have access.
    >


    class B: public A { ... };

    public derivation means that base class( A ) protected members can be
    used by members and friends of derived class( B ). How does above not
    break this rule?

    thanks - hunter
    Hunter Hou, Jun 18, 2004
    #4
  5. Andy Ward

    David Harmon Guest

    On Fri, 18 Jun 2004 16:51:22 +0800 in comp.lang.c++, Hunter Hou
    <> wrote,
    >David Harmon wrote:
    >
    >>
    >> Pointer a can point to A and other derived classes that are not B
    >> instances, therefore it would be wrong for B to have access.
    >>

    >
    >class B: public A { ... };
    >
    >public derivation means that base class( A ) protected members can be
    >used by members and friends of derived class( B ). How does above not
    >break this rule?


    public|protected|private derivation mean that protected members of A can
    be accessed from B when the A is actually the base class part of a B
    object. You do not get freebie access to mess around with the base
    class members of C objects, D objects, E objects or any other class that
    is foreign to B just because it also derives from A.

    I hope that is more clear; I don't know how else to say it.
    David Harmon, Jun 19, 2004
    #5
  6. Andy Ward

    Hunter Hou Guest

    David Harmon wrote:
    > On Fri, 18 Jun 2004 16:51:22 +0800 in comp.lang.c++, Hunter Hou
    > <> wrote,
    >
    >>David Harmon wrote:
    >>
    >>
    >>>Pointer a can point to A and other derived classes that are not B
    >>>instances, therefore it would be wrong for B to have access.
    >>>

    >>
    >>class B: public A { ... };
    >>
    >>public derivation means that base class( A ) protected members can be
    >>used by members and friends of derived class( B ). How does above not
    >>break this rule?

    >
    >
    > public|protected|private derivation mean that protected members of A can
    > be accessed from B when the A is actually the base class part of a B
    > object. You do not get freebie access to mess around with the base
    > class members of C objects, D objects, E objects or any other class that
    > is foreign to B just because it also derives from A.
    >
    > I hope that is more clear; I don't know how else to say it.
    >


    ok. I understand your meaning, but I think that's not convincing. In
    class A, I change protected to public, then everything is fine. No
    errors any longer.
    Hunter Hou, Jun 21, 2004
    #6
  7. * Andy Ward:
    > Given the following code:
    > class A
    > {
    > protected:
    > int pro;
    > };
    >
    > class B : public A
    > {
    > public:
    > void foo()
    > {
    > A * a;
    > a->pro;
    >
    > B * b;
    > b->pro2;
    > }
    > protected:
    > int pro2;
    > };
    >
    > The line b->pro2 compiles fine, however the line a->pro does not compile.
    > Could someone please explain to me why.


    A B-object can only access protected A-stuff in the A-part of B-objects.

    'protected' isn't a carte blanche to go messing around inside objects.

    It only makes the _definitions_ available, "known", to derived classes,
    and if you then create an object of such a derived class, say B, you
    (the class B code) can access all that you know in your own object.

    On the other hand there's no stopping the determined programmer from
    doing evil things.

    And sometimes that's necessary (e.g. getting access to the container
    inside a std::list).


    #include <iostream>

    class A
    {
    protected:
    int myValue;
    public:
    A( int aValue ): myValue( aValue ) {}
    };

    struct Hack: A
    {
    static int value( A const* p )
    {
    return static_cast<Hack const*>( p )->myValue;
    }
    };

    class B: public A
    {
    public:
    static int f()
    {
    A a( 1234 );

    // return a.myValue; -- does not compile.
    return Hack::value( &a ); // Oooh, dirty!
    }
    };

    int main()
    {
    std::cout << B::f() << std::endl; // "1234"
    }

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Jun 21, 2004
    #7
  8. Andy Ward

    Hunter Hou Guest

    Hunter Hou wrote:

    >>

    >
    > ok. I understand your meaning, but I think that's not convincing. In
    > class A, I change protected to public, then everything is fine. No
    > errors any longer.
    >


    please ignore this meesage.
    Hunter Hou, Jun 21, 2004
    #8
    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.

Share This Page