What is the 'this' pointer?

2019-01-06 15:07发布

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?

8条回答
可以哭但决不认输i
2楼-- · 2019-01-06 15:24

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.

查看更多
小情绪 Triste *
3楼-- · 2019-01-06 15:30

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.

查看更多
做自己的国王
4楼-- · 2019-01-06 15:37

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.

查看更多
相关推荐>>
5楼-- · 2019-01-06 15:42

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.

查看更多
别忘想泡老子
6楼-- · 2019-01-06 15:42

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).

查看更多
走好不送
7楼-- · 2019-01-06 15:44

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/

查看更多
登录 后发表回答