Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
What is the need of 'this' pointer in this example, if we can simply call the member function of the class with the help of objects then why we use 'this' pointer?

C++
#include <iostream> 
using namespace std; 
 
class MyClass { 
  int i; 
public: 
  void setI(int val) { 
    this->i = val; 
  }  
  int getI() { 
    return this->i; 
  }  
} ; 
  

int main() 
{ 
  MyClass o; 
 
  o.setI(100); 
  cout << o.getI(); 
 
  return 0; 
}
Posted
Updated 11-Aug-11 11:44am
v3

In your example you dont need this.
But:
C++
class CRect : protected RECT
{
public:
  CRect(RECT& rcinit){ CopyRect(this,&rcinit); }

  operator RECT& (){ return *this; }
};

Is a simple example you need this.
Regards.
 
Share this answer
 
we use this to sometimes remove confusions between variables.

for example :

class MyClass
{
  void foo();
  int i;
};

void MyClas::foo()
{
  int i;
  i = i; // confused ?
  i = this->i; // less confused ?

}
 
Share this answer
 
Comments
mbue 11-Aug-11 17:24pm    
No! Therefore the namespace operators are invented (MyClas::i) to resolve name conflicts.
Regards.
Emilio Garavaglia 12-Aug-11 2:17am    
but this->i remain the same even if you have to change the name of the class.
class::i doesn't.
There are language (D is and example) where even constructor and destructor are not name Class()/~Class but this() and ~this().
It is the old plain language use or pronoun versus nouns.
mbue 12-Aug-11 6:57am    
class A
{
public: int i;
};
class B : public A
{
public: int i;
void set(int i)
{
A::i = i;
B::i = i;
}
};

This is the right way. What matters if the name of class A changes? ;)
Emilio Garavaglia 13-Aug-11 5:58am    
There's no "right way". There is the C++ way, the C# way, the D way the VB way ... . What matters if name for A changes?? It depends on how many "changes" you have to track and how many of them may exist you're even not aware of. It is something that relates to the "industrial coding" and software maintenance. It is a common opinion between industrial coders that C++ has limitations in this field.
Most of the time, you don't need to. You only really need to use this to specify exactly which of two identically named variables you mean: the local / parameter one, or the class level / instance one. When you have this situation, using this forces recognition of class level variable, lack of it infers use of the local or parameter variable.

If there is no name conflict. then you don't need to use this, as the compiler will automatically insert it. But if you do, it's not an error - unnecessary, untidy and a bit "learner" - but not an error! :laugh:
 
Share this answer
 
It's use is optional. It's used to identify the parent object's instance, and is generally used to differentiate similrly-named variables in a method:

C++
public class XYZ
{
    public int x = 5;

    public void ProcessX(int x)
    {
        this->x += x;
    }    
}


In the code above, let's assume you call ProcessX with a value of 1. What will happen is that the variable x that is defined as a data memebr in the class will be incremented by the value of x that's passed to the method. The result will be:

C++
this->x = 6
x = 1


Some people like to use this, some don't. Sometimes you NEED to use it (like when you're calling a method that needs a pointer to the object that's calling the method. Generally, you should name your variables appropriately to prevent any confusion, and thus eliminate most of the need to use this.
 
Share this answer
 
In some cases you have to use 'this'. For example:
C++
class CChild
{
	CChild(CParent* pParent);
};
CCParent::CreateChild()
{
	CChild child(this);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900