Questions tagged [this-pointer]

The "this" pointer is a compiler generated pointer during a function call that points to the object upon which that function gets called.

Multiple objects of the same class have identical data members in them. Whenever a function gets called on an object, only the data members of that particular object are changed. The compiler knows which object's data members to access and modify by use of a special pointer, known as the this pointer, which stores the object's address in memory.

Whenever a function gets called on an object, the compiler automatically creates the this pointer and sends it to the function. Because of this, there is no need to explicitly declare or pass this to the function.

this is a keyword in the C++ language and is used in conjunction with the arrow member operator -> when used within a class to access its data members and functions.

void myClass::myFunction()
{
  this->myVariable = 10;
}

Which is equivalent to:

void myClass::myFunction()
{
  myVariable = 10;
}

Sometimes, the object itself is returned using the this pointer:

void myClass::myFunction()
{
  ...
  return (*this);
}
124 questions
114
votes
2 answers

std::shared_ptr of this

I am currently trying to learn how to use smart pointers. However while doing some experiments I discovered the following situation for which I could not find a satifying solution: Imagine you have an object of class A being parent of an object of…
Icarus
  • 1,455
  • 2
  • 11
  • 12
65
votes
6 answers

Where is the 'this' pointer stored in computer memory?

Where exactly is the 'this' pointer stored in memory? Is it allocated on the stack, in the heap, or in the data segment? #include using namespace std; class ClassA { int a, b; public: void add() { a =…
nagaradderKantesh
  • 1,582
  • 3
  • 16
  • 30
48
votes
12 answers

Is the "this" pointer just a compile time thing?

I asked myself whether the this pointer could be overused since I usually use it every single time I refer to a member variable or function. I wondered if it could have performance impact since there must be a pointer which needs to be dereferenced…
Yastanub
  • 1,189
  • 5
  • 18
34
votes
4 answers

Type of 'this' pointer

As mentioned in the title, I would like to know about the type of 'this' pointer. I'm working on a project and I observed that the type of 'this' pointer is "ClassName * const this" on windows using VC++ 2008. Well I would want to know what is the…
Purnima
  • 934
  • 1
  • 8
  • 15
27
votes
2 answers

When "this" is captured by a lambda, does it have to be used explicitly?

The examples I have found that capture this in a lambda use it explicitly; e.g.: capturecomplete = [this](){this->calstage1done();}; But it seems it is also possible to use it implicitly; e.g.: capturecomplete = [this](){calstage1done();}; I…
plugwash
  • 7,223
  • 1
  • 24
  • 40
25
votes
1 answer

If `this` is not const, why can't I modify it?

In The this pointer [class.this], the C++ standard states: The type of this in a member function of a class X is X*. i.e. this is not const. But why is it then that struct M { M() { this = new M; } }; gives error: invalid lvalue in…
Sebastian Mach
  • 36,158
  • 4
  • 87
  • 126
21
votes
1 answer

Const mismatches: 2 overloads have no legal conversion for 'this' pointer

I'm getting this weird error: error C2663: 'sf::Drawable::SetPosition' : 2 overloads have no legal conversion for 'this' pointer I think it has something to do with const mismatches but I don't know where, or why. In the following code I…
Griffin
  • 2,198
  • 6
  • 43
  • 78
21
votes
3 answers

Is it safe to return *this as a reference?

Returning reference to this object is often used in assignment operator overloading. It is also used as a base for named parameters idiom which allows to initialize object by chain of calls to setter methods: Params().SetX(1).SetY(1) each of which…
anton_rh
  • 5,990
  • 1
  • 31
  • 58
15
votes
6 answers

Is this[0] safe in C++?

This earlier question asks what this[0] means in C#. In C++, this[0] means "the zeroth element of the array pointed at by this." Is it guaranteed to not cause undefined behavior in C++ to refer to the receiver object this way? I'm not advocating…
templatetypedef
  • 328,018
  • 92
  • 813
  • 992
12
votes
4 answers

Can you explain the concept of the this pointer?

I need to understand this pointer concept, preferably with an example. I am new to C++, so please use simple language, so that I can understand it better.
Trupti
  • 567
  • 4
  • 8
  • 17
8
votes
4 answers

shared_ptr and the this-pointer

OK, I started using shared-pointers and pass shared-pointers as much as possible. No conversion to raw pointers anymore. This works good, except in this specific case: Suppose we have a class that also is an observer on another class, like…
Patrick
  • 22,097
  • 9
  • 57
  • 125
8
votes
2 answers

Why is this pointer needed when calling std::call_once()?

In book "C++ Concurrency in Action" §3.3.1, when introducing thread-safe lazy initialization of a class member using std::call_once(), it gives the following example: #include struct connection_info {}; struct data_packet {}; struct…
herohuyongtao
  • 45,575
  • 23
  • 118
  • 159
8
votes
2 answers

Not possible: this pointer as a default argument. Why?

The following code won't compile. Why? class A { int j; void f( int i = this->j ); } Edit, for clarity. This is what I was trying to do, using less lines of code... class A { void f( int i ){}; void f( ); int j; }; void A::f() { …
alexandreC
  • 481
  • 4
  • 13
7
votes
3 answers

ES6: this within static method

Let's say I have two ES6 classes like this: class Base { static something() { console.log(this); } } class Derived extends Base { } And then I make a call like this: Derived.something(); Note that I am making a call to a static…
treecoder
  • 36,160
  • 18
  • 57
  • 89
7
votes
2 answers

virtual method table for multiple-inheritance

I'm reading this article "Virtual method table" Example in the above article: class B1 { public: void f0() {} virtual void f1() {} int int_in_b1; }; class B2 { public: virtual void f2() {} int int_in_b2; }; class D : public B1, public B2…
Fihop
  • 2,977
  • 6
  • 36
  • 62
1
2 3
8 9