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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.