473,473 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

pointer to member usage ?

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
3 1346
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
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Mon | last post by:
I am in the process of reorganizing my code and came across and I came across a problem, as described in the subject line of this posting. I have many classes that have instances of other classes...
15
by: Albert | last post by:
Hi, I need to pass a pointer-to-member-function as a parameter to a function which takes pointer-to-function as an argument. Is there any way to do it besides overloading the function? Here...
7
by: Dave | last post by:
Hello all, In the code below, I use a pointer to an object under construction. Is the usage below legal? I have come across similar code at work. It compiles, but I'm not sure it's really...
1
by: zx.zhangxiong | last post by:
Dear all, I'm puzzled about the usage of class member function. Any help would be appreciated. class Account { ...
4
by: rahul8143 | last post by:
hello, I require a single c++ program that will illustrate usage of this,far,near pointers. Can any body please explain me those pointers? regards, rahul.
3
by: Dan Trowbridge | last post by:
Hi everyone, In my attempt to port code from VS 6.0 to VS.NET I had some code break along the way, mostly due to not adhereing closely to the C++ standard. This may be another instance but I...
8
by: toton | last post by:
HI, One more small doubt from today's mail. I have certain function which returns a pointer (sometimes a const pointer from a const member function). And certain member function needs reference...
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.