I know that "this" operator is used to access member functions of invoking object,but they can be called normally also .So,what exactly is the use of "this" operator.
-
Try reading the [standard](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf) it explains exactly what `this` is and when it is needed – Mgetz Jul 31 '13 at 17:17
-
He wants to know when this keyword is needed, not what it means. – Neil Kirk Jul 31 '13 at 17:17
-
Voting to close for not having a basic understanding is not fair. My interpretation is that the poster is basically asking "when *must* you use the this pointer," and there is an answer to that question. Moreover, most C++ programmers couldn't answer the question. That being said, this question has been asked and answered many times before, and is a dupe. It should be closed as a dupe. – John Dibling Jul 31 '13 at 17:18
-
This has been asked before in SO – David Rodríguez - dribeas Jul 31 '13 at 17:25
-
This question is closed for, IMO, the wrong reason. But it *should* be closed as a dupe, so I'm not going to vote to repoen. In any case, if you want to know when you *must* use the `this` pointer, here is the dupe I would have linked to: http://stackoverflow.com/questions/993352/when-should-i-make-explicit-use-of-the-this-pointer – John Dibling Jul 31 '13 at 17:27
-
@JohnDibling Not even there, there are alternatives :) – jrok Jul 31 '13 at 17:33
-
@jrok: I'm referring to dependant names, which is buried a bit. – John Dibling Jul 31 '13 at 17:39
-
@JohnDibling Me too. You can write either `this->member` or `someclass
::member`. – jrok Jul 31 '13 at 17:45 -
@jrok: aha! actually i think i knew that... – John Dibling Jul 31 '13 at 17:51
5 Answers
I don't think this is such a bad question. He is not asking what this does, but why the keyword exists when this is implied most of the time.
Here is a case where this pointer is necessary
class Test
{
int i;
void Set(int i)
{
this->i = i;
}
};
this is used to select between member variable and function parameter with the same name.
this is also used when you need the address of the object within the class.
void f(Test *p);
class Test
{
int i;
void pass()
{
f(this);
}
};

- 21,327
- 9
- 53
- 91
-
2Your second example is better, in the first one you could have qualified the name `Test::i`, as you do with any hiding scenario. – Ben Voigt Jul 31 '13 at 17:17
-
+1: Passing `this` as a function parameter is an obvious, everyday use. So obvious I didn't even think of it. – John Dibling Jul 31 '13 at 17:29
There is no operator called this
in C++ - there is an implicit variable that is present in all non-static member functions. Where this
is a pointer to the current instance of the class.
this
is also a keyword in the language (in other words, a reserved word, you can't use this
as a name of a variable or funciton)
this
can be used to refer to the object.
E.g.
class A
{
int x;
void some_func()
{
this->x = 42;
}
};
It is superfluous in the above case, but there are situations where it is useful or required.

- 99,718
- 31
- 186
- 324

- 126,704
- 14
- 140
- 227
First of all, this
is not an operator (operators are +
, -
, and the like). It is a reserved keyword, and also an expression.
this
is used in member functions to refer to the object into which the function is being called. You are right, it is not needed to refer to member variables or member functions, but it is useful if you want to refer to the whole object.

- 94,151
- 12
- 143
- 190
-
-
-
@BenVoigt: I'm referring to accessing non-static member functions in a base class template. – John Dibling Jul 31 '13 at 17:16
-
@John: In other words, yes, you're referring to dependent names (which may be functions or data members). That topic is so advanced compared to this question, and ISTR using `this` is only one of several possible ways to resolve those (so that the word "must" is a bit too strong). – Ben Voigt Jul 31 '13 at 17:19
-
@BenVoigt: Yes, in other words yes, but I was trying to give a little more so that other readers could do their own research. – John Dibling Jul 31 '13 at 17:20
-
@JohnDibling: I don't think that you'll help solving the OP doubts talking about dependent names name resolution. I'm trying to keep this as simple as possible. – rodrigo Jul 31 '13 at 17:39
'this' is a pointer to the instance of your object
Think about the following scenario where this can be used to see one benefit:
int add(int a, int b){
return a + b;
}
But lets say you also have a data member that is also named 'b'.
return a + b
Returns the sum of your arguments
return a + this->b
Returns the sum of your argument a and the data member b

- 343
- 3
- 8
- 18
-
A better solution to the name hiding problem is to select sane names for your members & parameters. – John Dibling Jul 31 '13 at 17:30
As per MSDN:
The this pointer (not operator) is a pointer accessible only within the nonstatic member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer.
You could use it when, let's say you want to make an assignments like these:
myClass::myClass(int x) {
myGlobalVar = this;
this-> x = x;
}

- 2,019
- 1
- 21
- 27