开发者

If I can take a normal pointer to a variable within a class, what's the point of class member pointers?

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;


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.


So that you can access the member on multiple instances.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜