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
2
votes
1 answer

Is `this` pointer assignable in c++?

when I try to assign a pointer to this pointer, My IDE(clion) said Expression is not assignable. So, Is this pointer assignable in c++ ? Is it a rvalue?
Flash
  • 21
  • 3
2
votes
2 answers

PHP assign $this of another class

I have been wondering is it possible to assign another object to $this? In CodeIgniter I am calling another controller from main controller. application/controllers/module.php Class Module extends CI_Controller { public function __construct() { …
Valour
  • 773
  • 10
  • 32
2
votes
1 answer

Doesn't static class members have no association with the this pointer?

Take this example: SomeClass.h class Foo { public: static int bar; int x; void someFunc() { this->x = 5; this->bar = 9; } }; SomeClass.cpp int Foo::bar = 0; mainc.pp #include #include "SomeClass.h" int…
Francis Cugler
  • 7,788
  • 2
  • 28
  • 59
2
votes
3 answers

Setting an instance of an object to another one from inside, using this = new Foo()?

I am working with a hash table and to rehash it, I am simply putting all the values into a new hash table, and then setting the executing instance to this new hash table. I wasn't sure going into it if that was possible, so I just want to confirm if…
Jomasi
  • 323
  • 1
  • 4
  • 14
2
votes
3 answers

Does this pointer adjustment occur for non-polymorphic inheritance?

Does non-polymorphic inheritance require this pointer adjustment? In all the cases I've seen this pointer adjustment discussed the examples used involved polymorphic inheritance via keyword virtual. It's not clear to me if non-polymorphic…
Praxeolitic
  • 22,455
  • 16
  • 75
  • 126
2
votes
7 answers

delete this pointer behaviour in g++

#include class Foo { public: Foo(char x); Foo(char x, int y); ~Foo(); void abc(); void dev(); }; void Foo::dev() { printf("inside dev…
cppfreak
  • 21
  • 2
2
votes
3 answers

Chaining commands by returning this pointer/reference in C# without assignment

This is mostly a syntax question. Here's a related thread showing different alternatives to achieve the same result: Method-Chaining in C# Now, in C++ it is possible to chain commands on an object by making methods return the pointer to the object…
2
votes
2 answers

Same this pointer and trouble with variadic types

I hope it's okay to just throw in a piece of code which I don't understand why it is behaving like it is. I have two problems with the following code. 1) Why is the this pointer for the two instances showing the same value? Here the output of the…
ritter
  • 7,447
  • 7
  • 51
  • 84
2
votes
4 answers

jQuery $(this) inside function

I want to pass $(this) to function but I am not sure. There is one similar thread, but I still can not make it working. I hope somebody can help me. $(document).ready(function() { var delay = (function(){ var timer = 0; return…
user1324762
  • 765
  • 4
  • 7
  • 24
2
votes
2 answers

After using 'delete this' in a member function I am able to access other member functions. Why?

I just wrote a sample program to see the behaviour of delete this class A { ~A() {cout << "In destructor \n ";} public: int a; A() {cout << "In constructor \n ";} void fun() { cout << "In fun \n"; delete this; …
Vikram Singh
  • 273
  • 1
  • 3
  • 10
1
vote
3 answers

Jquery - can't target the right element with $(this)

Possible Duplicate: $(this) doesn't work in a function Im having a problem targeting the right element in my code. I have a list of thumbnails on my page, and when you click on a "I dislike this" icon the targeted video change for another one.…
Lelly
  • 960
  • 3
  • 15
  • 29
1
vote
1 answer

Why is "this" in lambda capture treated differently than pointer to the same object?

If I capture the "this"-ptr in a lambda I can call member functions without problems. However, when I instead capture the pointer explicitely (without mentioning "this"), it stops working. Am I doing something wrong? According to my understanding,…
glades
  • 3,778
  • 1
  • 12
  • 34
1
vote
3 answers

An error while compare pointer with "NULL"

I got an error while "if (this == nullptr)" , the error : warning: nonnull argument 'this' compared to NULL [-Wnonnull-compare] 22 | if (this != nullptr) The command : g++ -std=c++11 -DNDEBUG -Wall .\avl.cpp Why can't I compare…
Algo
  • 185
  • 6
1
vote
0 answers

Calling a member function pointer in a template with implicit this

Looking at this answer I can see how to call a pointer to a member function by explicitly passing in this. However, what if I want the function passed in to be a member of the current object and to use the implicit this. I've written this, which…
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
1
vote
3 answers

Sending a class instance through a static reference member in a small chat client

I am building a small chat-room app in Java. What I am trying to do here is to send the current class ClientGUI instance (this) through a static ClientGUI reference member. The ServerApplication class is supposed to receive this current clientGUI…