Topic: Access to protected member in base class


Author: wmm@fastdial.net
Date: Mon, 30 Oct 2000 17:46:31 GMT
Raw View
In article <39EDF1AB.532634A3@fokus.gmd.de>,
  Christoph Reichert <reichert@fokus.gmd.de> wrote:
> I consider the following a bug in GCC-2.95.2, but I want to get shure
> befor reporting it as a bug.
>
> Consider the following simple definitions:
>
> ---
> class B {
> protected:
>         int i;
> };
>
> class D : public B {
> public:
>         void f(B *b) {
>                 b->i = 0;       // line 9: b->i not accessible ?!
>         }
> };
> ---
>
> Isn't this the case  member protection has been invented for ?

No, that's not a compiler bug.  In a member function of a
class with an inherited protected member, like your D::f,
you can only access that protected member using a pointer,
reference, or object whose type is the member function's
class or one derived from it.  That is, in your example,
the access would be permitted if D::f took an argument of
type D* instead of B*.

The reason for this restriction is that different derived
classes may use protected members differently.  Consider
a class D2 that is also derived from D.  You could pass a
pointer to a D2 object to D::f.  It might be the case that
D and D2 have different conventions regarding the use of
B::i -- for instance, D might only assign non-negative
values to B::i, but D2 might not obey that restriction.
If D::f were allowed to access B::i via the B* pointer that
was passed in and assumed that D's conventions were being
honored, a serious bug might result if a D2 were passed in
with B::i having a negative value.

This is obviously a contrived example, but Bjarne Stroustrup
recounts in the ARM a real situation where exactly this
confusion caused a major problem in a project written using
an earlier version of the language in which this restriction
was not enforced.  It was that experience that led to this
rule.

--
William M. Miller, wmm@fastdial.net
Vignette Corporation (www.vignette.com)


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: Christoph Reichert <reichert@fokus.gmd.de>
Date: 2000/10/19
Raw View
Dear  Experts,

I consider the following a bug in GCC-2.95.2, but I want to get shure
befor reporting it as a bug.

Consider the following simple definitions:

---
class B {
protected:
        int i;
};

class D : public B {
public:
        void f(B *b) {
                b->i = 0;       // line 9: b->i not accessible ?!
        }
};
---

B has a protected member i, and D is derived from B, so my understanding
of protected members is
that D::f should be allowed to access b::i via a pointer to B.

But I get the following error message:

c.cc: In method `void D::f(B *)':
c.cc:3: `int B::i' is protected
c.cc:9: within this context

Isn't this the case  member protection has been invented for ?

regards, chris

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: fjh@cs.mu.OZ.AU (Fergus Henderson)
Date: 2000/10/20
Raw View
Christoph Reichert <reichert@fokus.gmd.de> writes:

 >Dear  Experts,
 >
 >I consider the following a bug in GCC-2.95.2, but I want to get shure
 >befor reporting it as a bug.

It's not a bug.

 >Consider the following simple definitions:
 >
 >---
 >class B {
 >protected:
 >        int i;
 >};
 >
 >class D : public B {
 >public:
 >        void f(B *b) {
 >                b->i = 0;       // line 9: b->i not accessible ?!
 >        }
 >};
 >---
 >
 >B has a protected member i, and D is derived from B, so my understanding
 >of protected members is
 >that D::f should be allowed to access b::i via a pointer to B.

That's not correct.  D::f is allowed to access protected B members
of objects of type D.  But D::f is not allowed to access protected
B members of objects of type B or of other types derived from B.

See 11.5 "Protected member access" [class.protected] in the C++ standard.

--
Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3        |     -- the last words of T. S. Garp.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: Michiel Salters <salters@lucent.com>
Date: 2000/10/20
Raw View
Christoph Reichert wrote:

> I consider the following a bug in GCC-2.95.2, but I want to get shure
> befor reporting it as a bug.

Good thing - it's a bug in your program.

> Consider the following simple definitions:

> ---
> class B {
> protected:
>         int i;
> };

OK, any method of B as well as any method in a class of B can access this->i

> class D : public B {
> public:
>         void f(B *b) {
>                 b->i = 0;       // line 9: b->i not accessible ?!
>         }
> };
> ---

Ok, f(B *b) is a method of D. D is derived from B, thus D::f() may access
this->i.

> B has a protected member i, and D is derived from B, so my understanding
> of protected members is
> that D::f should be allowed to access b::i via a pointer to B.

Nope. Access specifiers like protected refer only to the internals of the
object itself, not to other objects of the same type.

> But I get the following error message:

> c.cc: In method `void D::f(B *)':
> c.cc:3: `int B::i' is protected
> c.cc:9: within this context

Right. The context isn't this->i, but b->i. That's an access violation.

> Isn't this the case  member protection has been invented for ?

No.

--
Michiel Salters

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: firstian@nospam.bellatlantic.net (Joe Chan)
Date: 2000/10/20
Raw View
A derived class object can only access its own protected members of the
base object. It can't reach into the protected members of another
object.

Christoph Reichert <reichert@fokus.gmd.de> wrote:

> Dear  Experts,
>
> I consider the following a bug in GCC-2.95.2, but I want to get shure
> befor reporting it as a bug.
>
> Consider the following simple definitions:
>
> ---
> class B {
> protected:
>         int i;
> };
>
> class D : public B {
> public:
>         void f(B *b) {
>                 b->i = 0;       // line 9: b->i not accessible ?!
>         }
> };
> ---
>
> B has a protected member i, and D is derived from B, so my understanding
> of protected members is
> that D::f should be allowed to access b::i via a pointer to B.
>
> But I get the following error message:
>
> c.cc: In method `void D::f(B *)':
> c.cc:3: `int B::i' is protected
> c.cc:9: within this context
>
> Isn't this the case  member protection has been invented for ?
>
> regards, chris
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
> [ Note that the FAQ URL has changed!  Please update your bookmarks.     ]


--
Joe Chan

Remove "nospam" to get my address.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: Jim Hyslop <jim.hyslop@leitch.com>
Date: 2000/10/20
Raw View
In article <39EDF1AB.532634A3@fokus.gmd.de>,
  Christoph Reichert <reichert@fokus.gmd.de> wrote:
> Dear  Experts,
>
> I consider the following a bug in GCC-2.95.2, but I want to get shure
> befor reporting it as a bug.
>
> Consider the following simple definitions:
>
> ---
> class B {
> protected:
>         int i;
> };
>
> class D : public B {
> public:
>         void f(B *b) {
>                 b->i = 0;       // line 9: b->i not accessible ?!
>         }
> };
> ---
>
> B has a protected member i, and D is derived from B, so my
> understanding
> of protected members is
> that D::f should be allowed to access b::i via a pointer to B.
You'll have to adjust your understanding, I'm afraid. :-)

The compiler is correct. A derived class cannot access protected members
of any arbitrary instance of a base class, only protected members of its
own base class, or protected members of the base class portion of a
sibling class.

The reason for this is simple. Suppose you have another derived class:

class D2 : public B
{
};

Suppose that D2 must not allow i to be 0.

If you write:
int main()
{
   D2 d2;
   D d;
   d.f(&d2);
}

and if your function f() did as you proposed, then you have just allowed
D to violate D2's invariant rules.

--
Jim
This message was posted using plain text only.  Any hyperlinks you may
see were added by other parties without my permission.
I do not endorse any products or services that may be hyperlinked to
this message.


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]