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

PHP $this use inside class to reference class

I have a function loadModule(); in class core, but to load modules I need to define variables in the construct, and many of them require core. Would I simply use loadModule("someModule", $settings, $dbc, $core, $etc...); or loadModule("someModule",…
Core
  • 335
  • 1
  • 11
0
votes
1 answer

Behavior of delete this pointer in C++

I am trying to explore this pointer in c++ . I have created a pointer to class object and caling a function to delete the current object but the behavior is unexpected. Why output of the last print statement is x= 0 , y= 6.…
Sunil Sharma
  • 163
  • 1
  • 15
0
votes
0 answers

ctypes callback function crashes Python - "this" pointer parameter?

I'm calling a C dll from Python using ctypes and having some trouble passing the correct parameters to the IBSU_RegisterCallbacks function, defined below. int WINAPI IBSU_RegisterCallbacks (const int handle, const IBSU_Events…
101
  • 8,514
  • 6
  • 43
  • 69
0
votes
2 answers

What happens when a member function with no arguments is called by an object in c++

Suppose we have a member function of class X and it is X f() which returns an object of class X and takes no arguments. So if it is called by an object of class X, say X obj is the object. So if we call obj.f(), so as per the C++ rules a secret…
0
votes
1 answer

For cascading member function calls, why do i need to return the reference? Why isn't just the this pointer sufficient?

#include using namespace std; class armon { int a; int b; public: armon(int newA, int newB) : a(newA), b(newB) {} armon setA(int newA) { a = newA; return *this; } armon setB(int newB) { …
Armon Safai
  • 175
  • 9
0
votes
2 answers

Using boost::shared_from_this for an object that sometimes isn't owned by shared_ptr

I want to use shared_from_this as follows: class example; // Forward declaration. void bar(boost::shared_ptr); class example : public boost::enabled_shared_from_this { void foo () { …
Subway
  • 5,286
  • 11
  • 48
  • 59
0
votes
2 answers

Send this instance as callback parameter

I have simple UI class and I need to send UI element instance to callbacks for each element, so I can (much like in javascript) manipulate element which called the callback. This requires to send instance of this to the function registered as…
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
0
votes
3 answers

constructor and this pointer

The question is: after declaration "private $json" should i use in constructor $json or $this->json ? class Controller{ private $json; private $data; function __construct(){ $json=json_decode(base64_decode($_POST['json'])); } }
0
votes
3 answers

How to apply overloaded operator to this?

How to call an overloaded operator in another member function of a class in C++ ?
Vincent
  • 57,703
  • 61
  • 205
  • 388
0
votes
1 answer

PHP Class properties being overwritten when I store to array

There is probably a very simple explanation for this, but I've had this code working for months, and now all of a sudden today it doesn't work. I retrieve all the rows from a table. I have and object which is an entity model of the table I'm…
T. Ujasiri
  • 317
  • 1
  • 3
  • 14
-1
votes
1 answer

read access violation. this was 0xCDCDCDCD

I'm confused as to what I'm doing wrong? While debugging, this shows 0xcdcdcdcd {theDouble=??? }so i know my variable isnt getting stored in my mutator. How would i go about fixing this issue? Am I supposed initialize this somewhere? I am using…
Iron Man
  • 7
  • 3
-1
votes
1 answer

template class multiple inheritance constructor with this pointer doesn't work?

I have a very basic code keeping in mind of java. I made a Object and Class class but in Template. Object.hpp #ifndef _OBJECT_HPP_ #define _OBJECT_HPP_ namespace library{ template class Object; template class Class; class…
-1
votes
1 answer

Use operator[] inside class that inherits std::vector

Doesn't work class A : public std::vector { explicit A() { push_back(5); std::cout << *this[0]; } } error: no match for 'operator*' (operand type is 'A') std::cout << *this[0];' Replacing *this[0] with at(0)…
Post Self
  • 1,471
  • 2
  • 14
  • 34
-2
votes
3 answers

"this" pointer (C++)

There is class definition like this: template FullO3CPU::FullO3CPU(DerivO3CPUParams *params) class DerivO3CPU : public FullO3CPU { public: DerivO3CPU(DerivO3CPUParams *p) : FullO3CPU(p) {…
mahmood
  • 23,197
  • 49
  • 147
  • 242
-2
votes
3 answers

Why was "this" used as a non const deprecated in C++

Why was this deprecated in C++? How is the this pointer in C++ different than this in Java? Or is Wikipedia just wrong Early versions of C++ would let the this pointer be changed; by doing so a programmer could change which object a method was…
Elpezmuerto
  • 5,391
  • 20
  • 65
  • 79
1 2 3
9
10