Where are pointers to class members used?

Started by
2 comments, last by Sneftel 15 years, 5 months ago

#include <iostream>

using namespace std;


class X
{
public:
	X(int i){x = i;}
	int x;
};

int main()
{
	int X::*data;
	X a(100);
	data = &X::x;

	cout << a.*data;

	cin.get();
	return 0;
}

How are these pointers used? I could use a normal pointer here and I wouldn't have to deal with that strange syntax.
Advertisement
When you want to modify a member of a class, but you don't know what member it's going to be. They're pretty uncommon.
The most common use I've seen is for function binding.

E.g:
struct Foo{    void frobnicate();};void call_frobnicate(std::vector<Foo> &foos){    std::for_each(foos.begin(),foos.end(),std::mem_fun_ref(&Foo::frobnicate));}

I haven't really come across situations where you use member pointers to non-functions.
Quote:Original post by rip-off
I haven't really come across situations where you use member pointers to non-functions.

You see it with some GUI libraries, where a GUI element is associated with a given class member, without referring to a specific object of that class.

This topic is closed to new replies.

Advertisement