what's the difference between using an ordinary pointer and a "pointer to member"?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 16
Reputation: kevintse is an unknown quantity at this point 
Solved Threads: 0
kevintse kevintse is offline Offline
Newbie Poster

what's the difference between using an ordinary pointer and a "pointer to member"?

 
0
  #1
Nov 23rd, 2008
  1. #include <iostream>
  2. using namespace std;
  3. #define PRINT(x) cout << #x " = " << x << endl;
  4.  
  5. class Object{
  6. public:
  7. Object():i(5){}
  8. int i;;
  9. };
  10.  
  11. int main() {
  12. Object o;
  13. o.i = 5;
  14. int* p = &(o.i);
  15. PRINT(*p);
  16.  
  17. int Object::* p2m = &Object::i;
  18. PRINT(o.*p2m);
  19. return 0;
  20. }

the two PRINT macros produce this:
  1. *p = 5
  2. o.*p2m = 5

could you tell me what the difference is between using them? when should i choose one over another? and why?
Thank you in advance!
Last edited by Ancient Dragon; Nov 23rd, 2008 at 10:16 pm. Reason: replaced quote tags with code tags
A little progress a day is what life has to be about.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: what's the difference between using an ordinary pointer and a "pointer to member"?

 
0
  #2
Nov 24th, 2008
Well ordinarily, your data in the class wouldn't be public to begin with.

Making it public defeats the whole purpose of having classes at all.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: what's the difference between using an ordinary pointer and a "pointer to member"?

 
0
  #3
Nov 24th, 2008
Read about:
- pointers to member functions:
http://www.gidforums.com/t-18332.html
- pointer to data members:
http://www.icce.rug.nl/documents/cpl...us15.html#l219
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC