什么是'this'指针?

What is the 'this' pointer?

本文关键字:指针 this 什么      更新时间:2023-10-16

我对c++相当陌生,我不明白this指针在以下场景中的作用:

void do_something_to_a_foo(Foo *foo_instance);

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

我从别人的帖子里抄的。

this指向什么?我困惑。函数没有输入,那么this在做什么?

this为当前对象。

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

简短的回答是,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;
}

当您编译并运行它时,可以观察到this的值在a1a2之间是不同的。

只是一些关于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

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

假设你有一个名为Car的类对象,它有一个非静态方法getColor(),在getColor()中调用这个方法会返回Car的地址(类的实例)。

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

表示调用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是通过引用传递的

Acc。用c++实现面向对象编程

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

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

非静态成员函数(如Foo::DoSomething)有一个隐式形参,其值用于this。标准在c++ 11§5.2.2/4中对此进行了规定:

调用函数时,每个形参(8.3.5)都要用对应的实参初始化(8.5,12.8,12.1)。如果函数是一个非静态成员函数,则该函数(9.3.2)的this形参应初始化为指向调用对象的指针,就像通过显式类型转换(5.4)一样进行转换。

因此,需要一个Foo对象来调用DoSomething。该对象直接变为this

this关键字与普通的显式声明的const指针参数之间的唯一区别(而且是微不足道的)是,您不能使用this的地址。

是一个局部指针。它将当前对象引用为本地对象