Sign in to follow this  
sheep19

Where are pointers to class members used?

Recommended Posts

sheep19    494
#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.

Share this post


Link to post
Share on other sites
rip-off    10979
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.

Share this post


Link to post
Share on other sites
Sneftel    1788
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this