What is the 'this' pointer?

I'm new to C ++, and I don't understand what the this pointer does in the following scenario:

 void do_something_to_a_foo(Foo *foo_instance); void Foo::DoSomething() { do_something_to_a_foo(this); } 

I grabbed this from someone else's message here.

What does this indicate? I'm confused. The function has no input, so what does this do?

+7
source share
9 answers

this refers to the current object.

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

+24
source

Short answer: this is a special keyword that identifies the "this" object - the one you are currently working on. A slightly longer, more complex answer is:

If you have a class , it can have member functions of two types: static and not static . Non- static member functions must work on a particular instance of the class, and they need to know where that instance is located. To help them, the language defines an implicit variable (that is, one that is automatically declared for you when it is needed, without the need for anything), called this , and that will be automatically made to point to a specific instance of the class on which member function is in effect.

Consider this simple example:

 #include <iostream> class A { public: A() { std::cout << "A::A: constructed at " << this << std::endl; } void SayHello() { std::cout << "Hi! I am the instance of A at " << this << std::endl; } }; int main(int, char **) { A a1; A a2; a1.SayHello(); a2.SayHello(); return 0; } 

When you compile and run this, note that the value of this is different from a1 and a2 .

+6
source

Just some random facts about this to complement other answers:

 class Foo { public: Foo * foo () { return this; } const Foo * cfoo () const { return this; /* return foo(); is an error */ } }; Foo x; // can call either x.foo() or x.cfoo() const Foo y; // can only call x.cfoo() 

When the object is const , this type becomes a pointer to const .


 class Bar { int x; int y; public: Bar () : x(1), y(2) {} void bar (int x = 3) { int y = 4; std::cout << "x: " << x << std::endl; std::cout << "this->x: " << this->x << std::endl; std::cout << "y: " << y << std::endl; std::cout << "this->y: " << this->y << std::endl; } }; 

The this pointer can be used to access a member that has been clouded by a function parameter or local variable.


 template <unsigned V> class Foo { unsigned v; public: Foo () : v(V) { std::cout << "<" << v << ">" << " this: " << this << std::endl; } }; class Bar : public Foo<1>, public Foo<2>, public Foo<3> { public: Bar () { std::cout << "Bar this: " << this << std::endl; } }; 

Multiple inheritance will result in different parents having different this values. Only the first inherited parent will have the same this value as the derived object.

+3
source

this one is a pointer to itself (the object that called this one ).

Suppose you have an object of class Car named car that has the non-stationary method getColor (), calling this inside getColor () returns the address of the car (an instance of the class).

Static member functions do not have a pointer to this (since they are not associated with an instance).

+2
source

this means the Foo object on which DoSomething () is called. I explain this with an example

 void do_something_to_a_foo(Foo *foo_instance){ foo_instance->printFoo(); } 

and our class

 class Foo{ string fooName; public: Foo(string fName); void printFoo(); void DoSomething(); }; Foo::Foo(string fName){ fooName = fName; } void Foo::printFoo(){ cout<<"the fooName is: "<<fooName<<endl; } void Foo::DoSomething(){ do_something_to_a_foo(this); } 

now we create objects of type

 Foo fooObject("first); f.DoSomething();//it will prints out first 

just as the string will be passed to the Foo constructor, it will be printed when DoSomething () is called.
For example, for example, in DoSomething () from the above example, β€œthis” means fooObject and in do_something_to_a_foo () fooObject is passed by reference.

+2
source

Non-static member functions, such as Foo::DoSomething , have an implicit parameter whose value is used for this . The standard defines this in C ++ 11 Β§5.2.2 / 4:

When a function is called, each parameter (8.3.5) must be initialized (8.5, 12.8, 12.1) with the corresponding argument. [Note: such initializations are vaguely ordered relative to each other (1.9) - end of note. If the function is a non-static member function, the function parameter (9.3.2) is initialized with a pointer to the call object, transformed as if by explicit conversion of type (5.4).

As a result, you will need a Foo object to call DoSomething . This object just becomes this .

The only difference (and trivial) between the this and the normal, explicitly declared const pointer parameter is that you cannot take the address of this .

+1
source

Acc. to object-oriented programming using C ++ by Balaguruswamy

this is a pointer pointing to the object for which the this function was called. For example, calling A.max() set the this pointer to the address of an object. The this pointer acts as an implicit argument for all member functions.

Here you will find a great example of this pointer. It also helped me understand the concept. http://www.learncpp.com/cpp-tutorial/8-8-the-hidden-this-pointer/

+1
source

This is a local pointer. It refers to the current object as a local object

0
source

A pointer is an object in a programming language whose value refers to dots for another value stored elsewhere in the computer's memory using its memory address.

You can also say that a pointer is a variable that has a memory address as its value.

0
source

All Articles