pointer to member usage ?

J

jjleto

I have just learn about "pointer to member". Does someone have an actual
example where "pointers to member" are usefull or necessary ? Can't
always &(myClass.member) be used ?

Thank you.
 
R

Rolf Magnus

jjleto said:
I have just learn about "pointer to member". Does someone have an actual
example where "pointers to member" are usefull or necessary ? Can't
always &(myClass.member) be used ?

Well, pointers do data members are not used very often AFAIK, and I don't
see a real use for them either. However, there are also pointers to member
functions, which can be useful, because you cannot use a regular function
pointer to point to a (non-static) member function.
 
D

Dietmar Kuehl

jjleto said:
I have just learn about "pointer to member". Does someone have an actual
example where "pointers to member" are usefull or necessary ? Can't
always &(myClass.member) be used ?

Assume you have a container of pairs, e.g.

typedef std::vector<std::pair<int, int> > Container;

.... and you want to print all 'first' elements on a row followed by
all 'second' elements on a row. You *can* code functions for printing
the first and second elements, e.g.:

| void printFirst(Container const& c) {
| for (Container::const_iterator it = c.begin(), end = c.end();
| it != end; ++it)
| std::cout << it->first << " ";
| std::cout << "\n";
| }

.... and likewise for 'printSecond()'. However, it would be easier to
have a common function which select the member to be printed using a
pointer to member:

| void print(Container const& c, int std::pair<int, int>::*mem) {
| for (Container::const_iterator it = c.begin(), end = c.end();
| it = end; ++it)
| std::cout << (*it).*mem << " ";
| std::cout << "\n";
| }

Actually, this function is still quite trivial and you might be happy
typing it twice: I'm not, especially as I suspect that I might want
to apply changes in the future (e.g. avoid the dangling space or change
the formatting) which I would have to do multiple time. Thus, I prefer
the additional parameter. Of course, things like this should probably
use an appropriate algorithm which in turn probably needs an
appropriate
functor but this one will use the pointer to member.

Another nice thing about pointer to member is that you the access rules
are not checked for the pointer. That is, although neither some
algorithm
nor some functor can access a private member directly, I can create the
pointer to member from a member function and hand it off to some
functor.
 
J

Jonathan Mcdougall

I have just learn about "pointer to member".

Good.
Does someone have an actual
example where "pointers to member" are usefull or necessary ?

The same examples for pointers to free functions apply.
Can't
always &(myClass.member) be used ?

What do you mean?


Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,536
Latest member
VeldaYoung

Latest Threads

Top