By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,165 Members | 850 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,165 IT Pros & Developers. It's quick & easy.

pointer to member usage ?

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
jjleto wrote:
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.
Jul 22 '05 #2

P: n/a
jjleto wrote:
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.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 22 '05 #3

P: n/a
> 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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.