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
1
vote
0 answers

memory reusing in c++? using placement new with this pointer inside constructor

i had one of these q asked in my test (the output). i searched a lot but unable to understand why #include using namespace std; class building{ public: building() { cout<<"geek building"<
1
vote
2 answers

Is it legal to refer to this pointer and hook up a lambda from inside the constructor of a class?

Scenario: I have class like below which hooks up a lambda in the constructor itself. And the lambda calls a class member function in turn. Code: class SomeClass { public: typedef std::function Callback; void…
AdeleGoldberg
  • 1,289
  • 3
  • 12
  • 28
1
vote
2 answers

How would you the "this" pointer of the base class from a method of a nested class?

I have a class nested in a base class, like so: class Base { public: void methodB() { std::cout << "method B"; } class Nested { public: void methodA() { methodB(); } }; Nested * member; }; This obviously generates a…
user8577930
  • 193
  • 2
  • 9
1
vote
5 answers

How does "this" pointer happen to point to different objects?

Suppose I have a class: class test { public: void print(); private: int x; }; void test::print() { cout<< this->x; } and I have these variable definitions: test object1; test object2; When I call object1.print() this happens to…
AvinashK
  • 3,309
  • 8
  • 43
  • 94
1
vote
2 answers

VueJS Error in v-on handler variable this. is undefined

Working in VueJS with MongoDB/Express backend api, attempting to add post a new record to the Bookmarks model with a single field url, but getting an error that the url field I am attempting to pass is undefined. I have used this pattern for api…
user2799827
  • 1,077
  • 3
  • 18
  • 54
1
vote
1 answer

Do I need the "this pointer" in my class methods?

Is there any difference between getA()&getB() and setA()&setB() ? If they are the same, which is the preferred syntax? class A{ public: int x; int getA(){return x;} int getB(){return this->x;} void setA(int…
arm.u
  • 55
  • 2
  • 7
1
vote
3 answers

'this' pointer, inheriting functions of super class in subclass using 'this' pointer

Hi i am trying to understand how to use the 'this' pointer. Now i wrote a sample program which uses a class Image which is a subclass of a class BMP. Now the functions TellWidth and TellHeight are declared in the BMP class. Now the compiler gives me…
1
vote
0 answers

Is there a way to save a class's this pointer into a member shared pointer

I have a structure like this: Foo.h template class Foo { public: // Public For Now While Constructing Class std::shared_ptr m_p256[256]; private: static unsigned m_elementCount; std::vector>>…
Francis Cugler
  • 7,788
  • 2
  • 28
  • 59
1
vote
0 answers

jQuery(this) weird propagation bug?

It looks like for some people this question is not clear. Desired effect: To remove the class of the element which has the mouse over it. Problem: The class removal affects to all the elements with same class even though I'm using jQuery(this) to…
user997593
  • 423
  • 5
  • 16
1
vote
1 answer

Constructors not setting this pointer to const causes undetected issues

Cutting my classes to the bare-minimum for readability: #ifndef MESSAGEFOLDER #define MESSAGEFOLDER #include #include class Message; class Folder{ public: void addMsg(Message* m) { messages.insert(m); } ~Folder() {…
AntiElephant
  • 1,227
  • 10
  • 18
1
vote
1 answer

This pointer is 0xfffffffc, potential causes?

I'm compiling the Crypto++ library at -O3. According to Undefined Behavior Sanitizer (UBsan) and Address Sanitizer (Asan), its OK. The program runs fine at -O2 (and -O3 on many platforms). Its also OK according to Valgrind under -O2. At -O3,…
jww
  • 97,681
  • 90
  • 411
  • 885
1
vote
1 answer

compiler's detail of this pointer and virtual functions

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
1
vote
1 answer

Cross-reference js-object variables when creating object

Summary: I want to know if it is possible to do something like this: {a: 'A',b: this.a} ...by using some other pointer like {a: 'A',b: self.a} or {a: 'A',b: own.a} or anything else... Full question: I'm trying to extend MyBaseModule using…
Ivar Bonsaksen
  • 4,747
  • 2
  • 32
  • 34
1
vote
2 answers

Pointer arithmetic on the "this" pointer

What are some legitimate and/or interesting uses for performing pointer-arithmetic on C++'s "this" pointer, if any? Just to make SE pleased with the length of this question, I'll include some relevant code. class Foo { public: Foo(bool terminate…
Whaa
  • 109
  • 6
1
vote
1 answer

Polymer this-pointer

I'm using the Polymer framework and I really enjoy it. But one thing I don't get is the confusion with the this-pointer. When functions get called from for example a button in your custom component the this-pointer points to the custom component.…
Peter Fortuin
  • 5,041
  • 8
  • 41
  • 69