Jump to content
  • Advertisement
Sign in to follow this  
sheep19

Where are pointers to class members used?

This topic is 3616 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!