0

Possible Duplicate:

When should I make explicit use of the `this` pointer?

In C++, what are the advantages of using the this pointer? i.e. Which of the following two snippets is preferred?

class stud
{
  int a;
  public:
  void run(int a) {
      stud::a=a;
  }
}; 

OR

 class stud { 
   int a; 
   public: 
   void run(int a){ 
     this->a=a;
    }
 };

Why the 1st case isn't used instead?

Community
  • 1
  • 1
  • Did you try compiling it? Are they really any different? – Carl Norum Feb 04 '13 at 18:02
  • No... they are the same. Exactly my question! Then why is *this* pointer used? What exactly is it's Purpose? – user2038517 Feb 04 '13 at 18:08
  • 2
    @user2038517: For occasions that are not this occasion. You can use `this` for many other things than accessing members. When you need it for those things, you'll know. You should really read the question of which yours has been marked a duplicate. – Lightness Races in Orbit Feb 04 '13 at 18:08
  • @user2038517: Interestingly, you seem to believe as the accepted answer in the linked question, that it does not matter. Well, it **does** matter in a few cases. In particular, the qualification inhibits dynamic dispatch to virtual functions. If `a` was a virtual function in your class, `this->a()` would call the final overrider, while `stud::a()` would call the override in the `stud` level (i.e. never to a more derived type). – David Rodríguez - dribeas Feb 04 '13 at 18:26

0 Answers0