什么是'this'指针?

时间:2013-05-11 00:57:56

标签: c++ class pointers this

我是C ++的新手,我不明白this指针在以下场景中的作用:

void do_something_to_a_foo(Foo *foo_instance);


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

我从别人的帖子里抓住了这个。

this指向什么?我糊涂了。该函数没有输入,那么this做什么?

8 个答案:

答案 0 :(得分:31)

this指的是当前对象。

关键字this标识一种特殊类型的指针。假设您创建了一个名为x class A的对象,class A具有非静态成员函数f()。如果您调用函数x.f(),则this正文中的关键字f()会存储x的地址。

答案 1 :(得分:8)

简短的回答是this是一个特殊的关键字,用于标识“此”对象 - 您当前正在操作的对象。更长,更复杂的答案是:

当您拥有class时,它可以包含两种类型的成员函数:static和非static。非static成员函数必须在类的特定实例上运行,并且需要知道该实例的位置。为了帮助他们,该语言定义了一个隐式变量(即在需要时自动为您声明而不需要执行任何操作的变量),该变量称为this,并且将自动指向特定实例成员函数正在运行的类。

考虑这个简单的例子:

#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;
}

编译并运行时,请注意thisa1之间a2的值不同。

答案 2 :(得分:3)

关于this的一些随机事实,以补充其他答案:

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

当对象为const时,this的类型将成为指向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;
    }
};

this指针可用于访问被函数参数或局部变量所掩盖的成员。


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; }
};

多重继承会导致不同的父级具有不同的this值。只有第一个继承的父项具有与派生对象相同的this值。

答案 3 :(得分:2)

是指向self的对象(调用的对象)。

假设你有一个名为car的类的对象,它有一个非静态方法getColor(),对getColor()内部的调用返回car的地址(类的实例)。

静态成员函数没有 this 指针(因为它们与实例无关)。

答案 4 :(得分:2)

这意味着调用DoSomething()的Foo对象。我用例子解释它

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

和我们班级

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);
}

现在我们实例化像

这样的对象
Foo fooObject("first);
f.DoSomething();//it will prints out first

类似于将传递给Foo构造函数的字符串将在调用DoSomething()时打印。
因为例如在上面示例的DoSomething()中,“this”表示fooObject,而在do_something_to_a_foo()中fooObject通过参考

答案 5 :(得分:1)

Foo::DoSomething等非静态成员函数具有隐式参数,其值用于this。该标准在C ++11§5.2.2/ 4中指定了这一点:

  

调用函数时,应使用相应的参数初始化每个参数(8.3.5)(8.5,12.8,12.1)。 [注意:这样的初始化相对于彼此不确定地排序(1.9) - 结束注释]如果函数是非静态成员函数,则函数(9.3.2)的this参数应初始化为指向调用对象的指针,转换为类似于显式类型转换(5.4)。

因此,您需要Foo个对象来呼叫DoSomething。该对象只是this

this关键字与正常明确声明的const指针参数之间的唯一区别(也是微不足道的)是您无法获取this的地址。

答案 6 :(得分:1)

  

度Acc。使用Balaguruswamy的c ++进行面向对象编程

this是一个指针,指向调用this函数的对象。例如,函数调用A.max()将指针this设置为对象的地址。指针this充当所有成员函数的隐式参数。

你会在这里找到一个很好的this指针示例。它也帮助我理解了这个概念。 http://www.learncpp.com/cpp-tutorial/8-8-the-hidden-this-pointer/

答案 7 :(得分:0)

它是一个本地指针。它将当前对象称为本地对象

相关问题