【问题标题】:What is the 'this' pointer?什么是'this'指针?
【发布时间】:2013-05-05 18:09:21
【问题描述】:

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

void do_something_to_a_foo(Foo *foo_instance);


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

我是从其他人的帖子中获取的。

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

【问题讨论】:

  • 所有非静态方法都有一个隐式的this 指针。在典型的 C++ 实现中,它在第一个整数参数槽中传递。
  • 我会避免做这样的事情 - 任何“对 foo 做某事”的函数都应该是 Foo 的成员函数/方法!
  • @Wayne Uroda:不一定。
  • @WayneUroda“可能”有点太强了。
  • “可能是 Foo 的一种方法的好候选人”怎么样?愿意为我提供一个反例吗? :)

标签: c++ class pointers this


【解决方案1】:

this 引用当前对象。

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

【讨论】:

    【解决方案2】:

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

    当你有一个class 时,它可以有两种类型的成员函数:static 和非static。非static 成员函数必须对类的特定instance 进行操作,并且它们需要知道该实例在哪里。为了帮助他们,该语言定义了一个名为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 之间是不同的。

    【讨论】:

      【解决方案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 值。

      【讨论】:

        【解决方案4】:

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

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

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

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            【解决方案6】:

            累加。 Balaguruswamy 用 c++ 进行面向对象编程

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

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

            【讨论】:

              【解决方案7】:

              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 的地址。

              【讨论】:

                【解决方案8】:

                是一个本地指针,将当前对象作为本地对象引用

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2010-10-13
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2011-09-30
                  • 2011-05-19
                  相关资源
                  最近更新 更多