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);
}
139 questions
7
votes
8 answers

Why the this-pointer address is something else than expected in the destructor (c++)

I have a weird problem with a this-pointer in a base-class destructor. Problem description: I have 3 classes: A1, A2, A3 A2 inherits publicly from A1 and inherits privately from A3 class A2:private A3, public A1 {...} A3 has a function…
AnorZaken
  • 1,916
  • 1
  • 22
  • 34
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
  • 3,127
  • 9
  • 42
  • 65
6
votes
3 answers

Reference to the this pointer: GCC vs clang

This is a follow-up of these questions. Consider the following code: struct A { private: A* const& this_ref{this}; }; int main() { A a{}; (void)a; } If compiled with the -Wextra, both GCC v6.2 and clang v3.9 show a warning. Anyway,…
skypjack
  • 49,335
  • 19
  • 95
  • 187
6
votes
2 answers

When to use THIS keyword when working with controls on form in C#

I am still far away from mastering C#, but the child in me is pushing me to continue improving my programming day by day. When I make a WinForms application I want to change and use lot of controls pragmatically. What I do not understand is when I…
adopilot
  • 4,340
  • 12
  • 65
  • 92
6
votes
3 answers

Is using "this" in contructor's initialization list specificly dangerous with Qt?

I need reliable information about "this" subject: class MyClass, public QWidget { public: MyClass( QWidget * parent = NULL ) :QWidget( parent ), mpAnotherWidget( new QWidget( this ) ){}; private: QWidget *…
LalaBox
  • 243
  • 2
  • 4
  • 10
5
votes
5 answers

restrict qualifier on member functions (restrict this pointer)

Note: To clarify, the question is not about the use of the restrict keyword in general, but specifically about applying it to member functions as described here. gcc allows you to use the __restrict__ (the GNU++ equivalent to C99's restrict)…
Damon
  • 67,688
  • 20
  • 135
  • 185
5
votes
2 answers

Assigning C++ function pointers to member functions of the same object

How do I get the function pointer assignments (and maybe the rest) in test.calculate to work? #include class test { int a; int b; int add (){ return a + b; } int multiply (){ return a*b; } …
toochin
  • 332
  • 1
  • 2
  • 9
5
votes
1 answer

C++ what is the value category of *this?

Section 9.3.2.1 of the C++ standard states: In the body of a non-static (9.3) member function, the keyword this is a prvalue expression whose value is the address of the object for which the function is called. The type of this in a member…
ThomasMcLeod
  • 7,603
  • 4
  • 42
  • 80
5
votes
4 answers

compiler's detail of this pointer, virtual function and multiple-inheritance

I'm reading Bjarne's paper: Multiple Inheritance for C++. In section 3, page 370, Bjarne said that "The compiler turns a call of a member function into an "ordinary" function call with an "extra" argument; that "extra" argument is a pointer to the…
Fihop
  • 3,127
  • 9
  • 42
  • 65
5
votes
1 answer

Return "this" as rvalue

The following code does, as expected, not compile #include class A { public: A() = default; ~A() = default; A(const A&) = delete; A(A&&) = delete; A& operator=(const A&) = delete; A& operator=(A&&) =…
mg84
  • 287
  • 2
  • 9
5
votes
4 answers

Using a ref Parameter with the this Keyword?

Is there a way to force the this keyword to act as a ref argument? I would like to pass in a visitor that modifies multiple properties on the object, but this only wants to act like a value parameter. Code in Object: public void…
grefly
  • 1,181
  • 2
  • 12
  • 28
5
votes
2 answers

Can the assignment of a shared_ptr trash the `this` pointer

Let's take the following example of a data structure (Node) that represents a tree of child nodes. The set of child nodes for each object is stored in a map> class Node; typedef std::shared_ptr NodePtr; class Node { std::map
selbie
  • 100,020
  • 15
  • 103
  • 173
5
votes
4 answers

C++ how to pass 'this' to pointer reference

i have main class that i like to pass its pointer reference to on of the objects i creating but it gives me error : Error 1 error C2664: 'GameController::GameController(GameLayer *&)' : cannot convert parameter 1 from 'GameLayer *const ' to…
user63898
  • 29,839
  • 85
  • 272
  • 514
5
votes
2 answers

How to get this pointer from std::function?

Since std::function can hold member functions, so it must store a pointer to the object instance somewhere. How can I fetch the this pointer from a std::function that holds a member function?
danijar
  • 32,406
  • 45
  • 166
  • 297
4
votes
1 answer

Is *this = Ctor(); legal and efficient for clearing an object's state?

I've stumbled across this piece of code to reestablish class invariants: class Foo { // some stuff in here public: void clear() { *this = Foo(); //operator=(Foo()); // commented out in favor of the line above } }; I would assume that…
andreee
  • 4,459
  • 22
  • 42
1
2
3
9 10