What Is the 'This' Pointer

What is the 'this' pointer?

this refers to the current object.

The keyword this identifies a special type of pointer. Suppose that you create an object named x of class A, and class A has a non-static member function f(). If you call the function x.f(), the keyword this in the body of f() stores the address of x.

Can you explain the concept of the this pointer?

this is a pointer to an instance of its class and available to all non-static member functions.

If you have declared a class, which has a private member foo and a method bar, foo is available to bar via this->foo but not to "outsiders" via instance->foo.

What does the this pointer mean?

'this' usually refers to the instance of the object that calls a particular method of a class,union,structure or a function.

when you have same names for different variables, then 'this' is used to differentiate between them.

class stu
{
int roll_no;
string name;

public:
void input(int roll_no,string name)
{
name=this->name;
roll_no=this->roll_no;
}


}

stu obj=new stu();
obj.input("47","harry");

Here, the 'this' tells that the 'name' is of the 'obj' that calls the method. Thus 'this' specifies the instance of the variable which belongs to the class object.
Also remember, when you want answers to theoretical questions, try to google them first.
Hope this helps.

C++ : Understanding this Pointer

I thought that "this" pointer refers to the value of the class object.

Correct. this always points to the object on which a member function is invoked.

I could see different values of "this" pointer

Oh, but you're not printing this (in globalA and globalB). You're printing obj which doesn't even have the same type as this.

this is of type concrete*. When you pass it to a function that takes an argument of type InterfaceB*, the pointer is implicitly converted to the other type. The conversion is allowed because interfaceB is a base of concrete. The converted pointer will no longer point to this object, but a base class sub object. A sub object (base class instance or a member) may but might not have the same address as the main object. Sub objects of an object cannot share an address, so at most one sub object may have the same address as the main object - except in the case of empty base optimization.

C++ `this` pointer

How this pointer is different from other pointers?

The this pointer only exists in the context of a non-static class member function. It is also implicit, it's name is a reserved keyword and it is always a prvalue expression. Otherwise, it's the same as any other pointer.

As I understand pointers point to the memory in heap.

Pointers can point to anything in memory. It's not limited to the heap and neither are objects.

Can we steal this pointer in move constructor or move assignment?

this is always a prvalue expression. It's not possible to assign a new address to it any more than you could assign a new value to 5. The fact is objects exist in one place in memory for their whole life time. Their address can never change and it would be illogical to try to change that by assigning a new address to this. Moving from an object moves the value or state that object has elsewhere, but the object itself still exists at it's previous address.

Is the this pointer just a compile time thing?

So is the this pointer just a compile time thing and not an actual pointer?

It very much is a run time thing. It refers to the object on which the member function is invoked, naturally that object can exist at run time.

What is a compile time thing is how name lookup works. When a compiler encounters x = X it must figure out what is this x that is being assigned. So it looks it up, and finds the member variable. Since this->x and x refer to the same thing, naturally you get the same assembly output.

this' keyword used in class and objects is a constant pointer?

According to 9.3.2 The this pointer [class.this]

1 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. [...]

So as the error says, the left hand side should be an lvalue but you're giving it a prvalue because change is a non-static member function and inside any non-static member function the keyword this is a prvalue according to the above quoted statement. Hence the error.

But you can modify the object that this points to, as shown below:

#include <iostream>
using namespace std;
class Test
{
private:
int x;
public:
Test(int x = 0) { this->x = x; }
void change(Test *t) { *this = *t; }//THIS WORKS
void print() { cout << "x = " << x << endl; }
};

int main()
{
Test obj(15);
Test *ptr = new Test (100);
obj.change(ptr);
obj.print();
return 0;
}

Note in the above example i have replaced this = t; with

*this = *t;//this time it works because now the left hand side is an lvalue 

This time the program works because now the left hand side is an lvalue.

Some More Explanation

From IBM's this pointer documentation,

The this parameter has the type Test *const. That is, it is a constant pointer to a Test object. Now since it is constant pointer, you cannot assign a different pointer value to it using this = t;. And hence the error.

Similarly, from Microsoft's this pointer documentation,

the this pointer is always a const pointer. It can't be reassigned.

So this also explains the error you're getting.



Related Topics



Leave a reply



Submit