Topic: Pointer [Was: Access] to protected member in base class


Author: wmm@fastdial.net
Date: Mon, 30 Oct 2000 20:41:04 GMT
Raw View
In article <1eittws.1cotn78kpslc0N%joerg.barfurth@attglobal.net>,
  joerg.barfurth@attglobal.net (Joerg Barfurth) wrote:
> Which reminds another issue. Why does an expression of the form
> '&Derived::member_of_Base' have the type pointer-to-member-of-Base ?
...
>  (BTW: I was surprised when I learned that this is not the case, and
> I've seen others taken by surprise as well - especially together with
> template argument deduction).
>
> NOTE: I know that this is an incompatible change and therefore
unlikely
> to get into standard, even in the future, but still ... I think that
the
> current version of 5.3.1/2 is unsatisfactory.
>
> What do others think ?

This is an open issue on the core language issues list (#203).

--
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: joerg.barfurth@attglobal.net (Joerg Barfurth)
Date: Sun, 22 Oct 2000 01:25:31 GMT
Raw View
Fergus Henderson <fjh@cs.mu.OZ.AU> wrote:

> Christoph Reichert <reichert@fokus.gmd.de> writes:
>=20
>  >Consider the following simple definitions:
>  >
>  >---
>  >class B {
>  >protected:
>  >        int i;
>  >};
>  >
>  >class D : public B {
>  >public:
>  >        void f(B *b) {
>  >                b->i =3D 0;       // line 9: b->i not accessible ?!
>  >        }
>  >};
>  >---

> 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.
>=20
> See 11.5 "Protected member access" [class.protected] in the C++ standar=
d.

Which reminds another issue. Why does an expression of the form
'&Derived::member_of_Base' have the type pointer-to-member-of-Base ?

How does protected relate to this ? As a reminder, in the following

  class B=20
  {
  protected:
     int i;
  public:
     int j;
  };

  struct D : B
  {
     int f(B *b)
     {
        int B::*pb =3D &B::j; // (1) OK
        int D::*pd =3D &B::j; // (2) OK

        int B::*qb =3D &D::j; // (3) OK !?
        int D::*qd =3D &D::j; // (4) OK

        int B::*rb =3D &B::i; // (5) ERROR !
        int D::*rd =3D &D::i; // (6) OK !
        int B::*sd =3D &D::i; // (7) OK ??

        return b->*sd;
     }
  };

line (5) is illegal, which is fine and safe.
line (6) is legal, which is fine and mostly safe
but therefore line (7) is allowed, (as is line (3))
=20
OTOH, if the type of &D::i and &D::j were int D::* instead of int B::*
this would disallow lines (3) and (7). Disallowing (7) is fine, and
using (1) helps around the missing case (3). Only if this were the case,
would the special provision about the syntax for specifying pointers to
protected members in 11.5 make sense.

 (BTW: I was surprised when I learned that this is not the case, and
I've seen others taken by surprise as well - especially together with
template argument deduction).

NOTE: I know that this is an incompatible change and therefore unlikely
to get into standard, even in the future, but still ... I think that the
current version of 5.3.1/2 is unsatisfactory.

What do others think ?

Regards, J=F6rg

--=20
J=F6rg Barfurth                         joerg.barfurth@attglobal.net

---
[ 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.     ]