user2371809
user2371809

Reputation: 351

What is the 'this' pointer?

I'm fairly 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 that from someone else's post on here.

What does this point to? I'm confused. The function has no input, so what is this doing?

Upvotes: 18

Views: 44298

Answers (8)

Rajat Garg
Rajat Garg

Reputation: 542

Acc. to Object Oriented Programming with c++ by Balaguruswamy

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

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

Upvotes: 2

Abhishek Kumar
Abhishek Kumar

Reputation: 1

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

Upvotes: 0

TelKitty
TelKitty

Reputation: 3156

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.

Upvotes: 34

jxh
jxh

Reputation: 70392

Just some random facts about this to supplement the 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, the type of this 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 was overshadowed by a function parameter or a 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 cause the different parents to have different this values. Only the first inherited parent will have the same this value as the derived object.

Upvotes: 3

Potatoswatter
Potatoswatter

Reputation: 137780

Nonstatic member functions such as Foo::DoSomething have an implicit parameter whose value is used for this. The standard specifies this in C++11 §5.2.2/4:

When a function is called, each parameter (8.3.5) shall be initialized (8.5, 12.8, 12.1) with its corresponding argument. [Note: Such initializations are indeterminately sequenced with respect to each other (1.9) — end note ] If the function is a non-static member function, the this parameter of the function (9.3.2) shall be initialized with a pointer to the object of the call, converted as if by an explicit type conversion (5.4).

As a result, you need a Foo object to call DoSomething. That object simply becomes this.

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

Upvotes: 1

Nik Bougalis
Nik Bougalis

Reputation: 10613

The short answer is that this is a special keyword that identifies "this" object - the one on which you are currently operating. The slightly longer, more complex answer is this:

When you have a class, it can have member functions of two types: static and non-static. The non-static member functions must operate on a particular instance of the class, and they need to know where that instance is. To help them, the language defines an implicit variable (i.e. one that is declared automatically for you when it is needed without you having to do anything) which is called this and which will automatically be made to point to the particular instance of the class on which the member function is operating.

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, observe that the value of this is different between a1 and a2.

Upvotes: 9

Aqeel Raza
Aqeel Raza

Reputation: 1829

this means the object of Foo on which DoSomething() is invoked. I explain it with 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 instantiate objects like

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

similarly whatever the string will be passed to Foo constructor will be printed on calling DoSomething().
Because for example in DoSomething() of above example "this" means fooObject and in do_something_to_a_foo() fooObject is passed by reference.

Upvotes: 2

AngelCastillo
AngelCastillo

Reputation: 2435

this is a pointer to self (the object who invoked this).

Suppose you have an object of class Car named car which have a non static method getColor(), the call to this inside getColor() returns the adress of car (the instance of the class).

Static member functions does not have a this pointer(since they are not related to an instance).

Upvotes: 2

Related Questions