Accessing a protected member of a member of type BaseClass????

Discussion in 'C++' started by Steven T. Hatton, Aug 16, 2004.

  1. I find the surprising. If I derive Rectangle from Point, I can access the
    members of Point inherited by Rectangle _IF_ they are actually members of a
    Rectangle. If I have a member of type Point in Rectangle, the compiler
    tells me Point::x is protected. I would have expected Rectangle to see the
    protected members of any Point. Compiling the following code give me this
    error:
    g++ -o rectangle main.cc
    main.cc: In member function `size_t Rectangle::dx()':
    main.cc:22: error: `size_t Point::y' is protected
    main.cc:32: error: within this context

    #include <iostream>
    #include <cstddef> //size_t

    class Point {

    public:
    Point(const size_t& x_=0,
    const size_t& y_=0)
    : x(x_),
    y(y_)
    {}

    Point& operator+(const Point& p)
    {
    this->x += p.x;
    this->y += p.y;
    return *this;
    }

    protected:
    size_t x;
    size_t y; //line 22
    };

    class Rectangle : public Point {
    public:
    Rectangle(const Point& xy, const Point& dxdy_)
    : Point(xy),
    dxdy(dxdy_)
    {}
    size_t X() const { return this->x; }
    size_t dx() const { return this->dxdy.y; } //line 32
    protected:
    Point dxdy;
    };
    int main(){

    }

    ISO/IEC 14882 says this:

    11.2 Accessibility of base classes and base class members

    [class.access.base]

    "If a class is declared to be a base class (clause 10) for another class
    using the public access specifier, the public members of the base class are
    accessible as public members of the derived class and protected members of
    the base class are accessible as protected members of the derived class."

    The way I read that, I should be able to access Rectangle::dxdy.y from
    within Rectangle. Am I missing something, or is g++ wrong?
    --
    STH
    Hatton's Law: "There is only One inviolable Law"
    KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
    Mozilla: http://www.mozilla.org
    Steven T. Hatton, Aug 16, 2004
    #1
    1. Advertising

  2. Answered?: Accessing a protected member of a member of type BaseClass????

    Steven T. Hatton wrote:

    > I find the surprising. If I derive Rectangle from Point, I can access the

    ....

    This looks like an answer. I'm not accessing Point::dxdy through a pointer
    to Rectangle. I would need to get really brutal with a cast to do so, and
    I'm not sure that would even work(correctly/reliably).

    http://publib.boulder.ibm.com/infoc...cpp6a.doc/language/ref/exceptions.cplr162.htm

    A protected nonstatic base class member can be accessed by members and
    friends of any classes derived from that base class by using one of the
    following:

    * A pointer to a directly or indirectly derived class
    * A reference to a directly or indirectly derived class
    * An object of a directly or indirectly derived class

    If a class is derived privately from a base class, all protected base class
    members become private members of the derived class.

    If you reference a protected nonstatic member x of a base class A in a
    friend or a member function of a derived class B, you must access x through
    a pointer to, reference to, or object of a class derived from A. However,
    if you are accessing x to create a pointer to member, you must qualify x
    with a nested name specifier that names the derived class B. The following
    example demonstrates this:

    class A {
    public:
    protected:
    int i;
    };


    class B : public A {
    friend void f(A*, B*);
    void g(A*);
    };

    void f(A* pa, B* pb) {
    // pa->i = 1;
    pb->i = 2;

    // int A::* point_i = &A::i;
    int A::* point_i2 = &B::i;
    }

    void B::g(A* pa) {
    // pa->i = 1;
    i = 2;

    // int A::* point_i = &A::i;
    int A::* point_i2 = &B::i;
    }

    void h(A* pa, B* pb) {
    // pa->i = 1;
    // pb->i = 2;
    }

    int main() { }

    Class A contains one protected data member, an integer i. Because B derives
    from A, the members of B have access to the protected member of A. Function
    f() is a friend of class B:

    * The compiler would not allow pa->i = 1 because pa is not a pointer to
    the derived class B.
    * The compiler would not allow int A::* point_i = &A::i because i has
    not been qualified with the name of the derived class B.

    Function g() is a member function of class B. The previous list of remarks
    about which statements the compiler would and would not allow apply for g()
    except for the following:

    * The compiler allows i = 2 because it is equivalent to this->i = 2.

    Function h() cannot access any of the protected members of A because h() is
    neither a friend or a member of a derived class of A.


    --
    STH
    Hatton's Law: "There is only One inviolable Law"
    KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
    Mozilla: http://www.mozilla.org
    Steven T. Hatton, Aug 16, 2004
    #2
    1. Advertising

  3. Steven T. Hatton

    tom_usenet Guest

    On Mon, 16 Aug 2004 07:15:24 -0400, "Steven T. Hatton"
    <> wrote:

    >I find the surprising. If I derive Rectangle from Point, I can access the
    >members of Point inherited by Rectangle _IF_ they are actually members of a
    >Rectangle. If I have a member of type Point in Rectangle, the compiler
    >tells me Point::x is protected.


    Yes, that's correct behaviour, and is required to maintain
    encapsulation.

    > I would have expected Rectangle to see the
    >protected members of any Point.


    But that would allow it to break instances of completely unrelated
    classes that just happen to also derive from Point. For example,
    perhaps Trapezium derives from point; we don't want Rectangle to be
    able to modify the Point subobject of a Trapezium.

    >ISO/IEC 14882 says this:
    >
    >11.2 Accessibility of base classes and base class members
    >
    >[class.access.base]
    >
    >"If a class is declared to be a base class (clause 10) for another class
    >using the public access specifier, the public members of the base class are
    >accessible as public members of the derived class and protected members of
    >the base class are accessible as protected members of the derived class."
    >
    >The way I read that, I should be able to access Rectangle::dxdy.y from
    >within Rectangle. Am I missing something, or is g++ wrong?


    See 11.5/1

    Tom
    tom_usenet, Aug 16, 2004
    #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. verbatime
    Replies:
    7
    Views:
    339
    John Harrison
    Mar 4, 2004
  2. andy
    Replies:
    2
    Views:
    336
    Dietmar Kuehl
    Mar 17, 2005
  3. Harald Massa

    overwriting method in baseclass

    Harald Massa, Feb 7, 2005, in forum: Python
    Replies:
    5
    Views:
    320
    Harald Massa
    Feb 9, 2005
  4. NewToCPP
    Replies:
    3
    Views:
    371
    NewToCPP
    Aug 30, 2006
  5. blangela
    Replies:
    8
    Views:
    651
    Erik Wikström
    Sep 26, 2008
Loading...

Share This Page