views:

78

answers:

3

Take this code:

struct mystruct
{
    int var;

    mystruct() : var(0) {}
};

int main()
{
    mystruct ins;

    int* p = &ins.var;

    *p = 1;
}

So what are some concrete really good examples of uses for the class member pointer?

int    X::*p = &X::data; /* p contains offset */
X      object;
X      *objptr = new X;

int    i = object.*p;
int    j = objptr->*p;
+3  A: 

So that you can access the member on multiple instances.

SLaks
+6  A: 

It seems the your question is about pointers of pointer-to-data-member type. In C++ there are also pointers of pointer-to-member-function type. The two have something in common at some abstract level, but otherwise they are different.

Pointer of pointer-to-data-member type is applicable to any instance of the class. An ordinary pointer always points to a member of a specific instance of the class. Pointer of pointer-to-data-member type is a higher level implementation of the idea of "run-time offset" from the beginning of a class object. It is a relative pointer. In that way it is completely different thing from ordinary pointers, which are absolute pointers.

To illustrate that with an example, let's say you have an array of classes with three members of the same type x, y and z

struct Point { int x, y, z; };

Point point_array[N];

and you need to set all members with the same name to 0 in the entire array, without changing any other members. Here's how you can do it using a pointer of pointer-to-data-member type

void zero_members(Point points[], int n, int Point::*m) {
  for (int i = 0; i < n; ++i)
    points[i].*m = 0;
}

Now, by using this function you can do

zero_members(point_array, N, &Point::x);

to set all xs to zero. Or you can do

zero_members(point_array, N, &Point::y);

to set all ys to zero. You do can all this with a single function, and what's also important, the member selection is performed by a run-time parameter (as opposed to a compile-time one).

You cannot do something like this with ordinary pointers. In fact, you can't do it in any other way.

AndreyT
+2  A: 

Uhm, this is akin to asking for good examples of using goto... ;-) Okay, it's there, and sometimes it can be handy, but any example of "good" use of goto is likely to be disputed. And so also for member pointers, they're pretty low-level.

For member function pointers one common usage is in event-driven systems. Package a member function pointer together with a pointer to an object of the relevant class, and you have something very much like a C# delegate. You can then pass this little functor package around, and someone else's code, e.g. a GUI framework, can call back on your object without even knowing about it.

The Boost library provides some support for that, e.g. boost::function and boost::bind, and so does TR1 and the upcoming C++0x standard library (essentially a subset of the Boost functionality).

I can't offhand think of any common usage of data member pointers, though.

Regarding "low level", member pointers follow special rules which let you inadvertently bypass a protected access restriction.

Cheers & hth.,

– Alf

Alf P. Steinbach
They are higher-level than regular pointers, or the closest alternative `offsetof`. (For evidence, note that pointer-to-member types are larger than actual pointers.) Bypassing access restrictions with a PTM is no different from exposing a pointer to a private instance member. There are no special rules, and I can't imagine doing that accidentally.
Potatoswatter
@Potatoswatter: Hi. The particular special rule I referred to is C++98 §4.11/2. Searching for a discussion of this the first concrete example I found was [http://groups.google.com/group/comp.lang.c++.moderated/browse_frm/thread/100c8b0851250de/04fb4a8884b2d6b9], by me. The first hit Google served up, however, was [http://groups.google.com/group/comp.lang.c++.moderated/browse_frm/thread/113d27ced1bdcaa7/6e569f4e17eb63da]. ;-) Regarding abstraction levels, I don't find it meaningful to compare higher/lower level between regular pointers and member pointers.
Alf P. Steinbach