DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Need Help with a question (http://www.daniweb.com/forums/thread138898.html)

sahmedsa Aug 6th, 2008 5:31 pm
Need Help with a question
 
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

Duoas Aug 6th, 2008 5:47 pm
Re: Need Help with a question
 
this is a special pointer that points to the active object. For example:
#include <iostream>
#include <string>
using namespace std;

class WhoAmI
  {
    string name;
  public:
    WhoAmI( const string& name ): name( name ) { }
    void print( const WhoAmI& other )
      {
      cout << "I am " << this->name << endl;
      if (this == &other)
        cout << "I am beside myself.\n";
      else
        cout << "I am with " << other.name << endl;
      }
  };

int main()
  {
  WhoAmI george( "George" );
  WhoAmI alannah( "Alannah" );

  george.print( george );
  alannah.print( george );

  return 0;
  }
Hope this helps.


All times are GMT -4. The time now is 6:30 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC