Topic: pointers to members with reference type


Author: Alan@doughnut.demon.co.uk (Alan Bellingham)
Date: Wed, 16 Nov 1994 17:23:49 +0000
Raw View
In article: <KUHLINS.94Nov14152939@hawk.wifo.uni-mannheim.de>  kuhlins@hawk.wifo.uni-mannheim.de
(Stefan Kuhlins) writes:
>
>
> WP r.8.3.3 pointers to members
>
>   Note that a pointer to member cannot point to
>   - a static member of a class,
>   - a member with reference type,
>   - or "cv void".
>
> Why not to a member with reference type?
> A reference is a kind of a const pointer, and a pointer to a member
> which is a const pointer is ok (tested with SUN CC 4.0).
> What have I missed?

If your pointer to member points to a pointer, it points to the pointer,
not to where the pointer points.

It is reasonable to implement the pointer to member as an offset pointer
into the object. However, a static member isn't inside the object, and
nor (probably) is the subject of a reference member.

As I see it, there are three courses open here:

1) Make the language do it anyway (makes for more complexity for compiler
writers)

2) Invent pointer-to-static-member and pointer-to-reference-member types as
well (not _more_ types)

3) Just say no

As to the use of pointers to members? Well, I haven't directly used them
myself, but they're used heavily inside the Microsoft Foundation Classes,
which I use.

Alan Bellingham
--
Alan@doughnut.demon.co.uk    "... not many creatures are twenty feet across,
Alan@episys.win-uk.net       one inch thick and deep fried." Terry Pratchett
ACCU: Association of C & C++ Users                    First ever Elite pilot





Author: kuhlins@hawk.wifo.uni-mannheim.de (Stefan Kuhlins)
Date: 14 Nov 1994 14:29:39 GMT
Raw View
/*
WP r.8.3.3 pointers to members

  Note that a pointer to member cannot point to
  - a static member of a class,
  - a member with reference type,
  - or "cv void".

Why not to a member with reference type?
A reference is a kind of a const pointer, and a pointer to a member
which is a const pointer is ok (tested with SUN CC 4.0).
What have I missed?

BTW do you know a good example where pointers to members are needed?

Thanks for your time!
  - Stefan
*/

#include <iostream.h>

class X {
public:
  X(int& r) : x(10765), y(r), z(&r) {}
  int x;
  int& y;
  int*const z;
};

int main() {
  int X::*p = &X::x;
  int*const X::*r = &X::z;

  int i = 4711;
  X x(i);

  cout << x.*p << endl;
  cout << *(x.*r) << endl;

//  int& X::*q = &X::y;   // error
//  cout << x.*q << endl;
  return 0;
}




Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 14 Nov 1994 19:24:41 GMT
Raw View
In article 94Nov14152939@hawk.wifo.uni-mannheim.de, kuhlins@hawk.wifo.uni-mannheim.de (Stefan Kuhlins) writes:
>
>/*
>WP r.8.3.3 pointers to members
>
>  Note that a pointer to member cannot point to
>  - a static member of a class,
>  - a member with reference type,
>  - or "cv void".
>
>Why not to a member with reference type?
>A reference is a kind of a const pointer, ...

No, it is not. References are implemented like pointers, but references are
not pointers.

References are not lvalues in C++. You cannot assign to a reference or get
its address (as opposed to the thing it references). You cannot create a
type "pointer-to-reference" or "array-of-reference" for those reasons. Thus
you also cannot have a pointer-to-reference-member. It would be very
inconsistent if you could.

---
Steve Clamage, stephen.clamage@eng.sun.com