Need Help with a question

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

Join Date: Jul 2008
Posts: 4
Reputation: sahmedsa is an unknown quantity at this point 
Solved Threads: 0
sahmedsa sahmedsa is offline Offline
Newbie Poster

Need Help with a question

 
0
  #1
Aug 6th, 2008
I always see solutions to some questions regarding classes, and they have (*this) , I don't understand when to use it and how to use it. Does it just mean that we're referring to something? Please help me out. (an explaination with an example would be helpful)

Thanks
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Need Help with a question

 
0
  #2
Aug 6th, 2008
this is a special pointer that points to the active object. For example:
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class WhoAmI
  6. {
  7. string name;
  8. public:
  9. WhoAmI( const string& name ): name( name ) { }
  10. void print( const WhoAmI& other )
  11. {
  12. cout << "I am " << this->name << endl;
  13. if (this == &other)
  14. cout << "I am beside myself.\n";
  15. else
  16. cout << "I am with " << other.name << endl;
  17. }
  18. };
  19.  
  20. int main()
  21. {
  22. WhoAmI george( "George" );
  23. WhoAmI alannah( "Alannah" );
  24.  
  25. george.print( george );
  26. alannah.print( george );
  27.  
  28. return 0;
  29. }
Hope this helps.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
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