#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;
}
Where are pointers to class members used?
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:
I haven't really come across situations where you use member pointers to non-functions.
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
Popular Topics
Advertisement