What is purpose of a “this” pointer in C++? [dupli

2020-05-25 07:53发布

What is purpose of this keyword. Doesn't the methods in a class have access to other peer members in the same class ? What is the need to call a this to call peer methods inside a class?

17条回答
地球回转人心会变
2楼-- · 2020-05-25 08:15

Some points to be kept in mind

  • This pointer stores the address of the class instance, to enable pointer access of the members to the member functions of the class.

  • This pointer is not counted for calculating the size of the object.

  • This pointers are not accessible for static member functions.

  • This pointers are not modifiable

Look at the following example to understand how to use the 'this' pointer explained in this C++ Tutorial.

class this_pointer_example // class for explaining C++ tutorial 
{
    int data1;
 public:
    //Function using this pointer for C++ Tutorial
    int getdata()
    { 
        return this->data1;
    } 
  //Function without using this pointer 
  void setdata(int newval)
  {
       data1 = newval;
  }
};

Thus, a member function can gain the access of data member by either using this pointer or not. Also read this to understand some other basic things about this pointer

查看更多
Explosion°爆炸
3楼-- · 2020-05-25 08:16

The expression *this is commonly used to return the current object from a member function:

return *this;

The this pointer is also used to guard against self-reference:

if (&Object != this) {
// do not execute in cases of self-reference
查看更多
在下西门庆
4楼-- · 2020-05-25 08:20

The this pointer inside a class is a reference to itself. It's needed for example in this case:

class YourClass
{
   private:
      int number;

   public:
      YourClass(int number)
      {
         this->number = number;
      }
}

(while this would have been better done with an initialization list, this serves for demonstration)

In this case you have 2 variables with the same name

  1. The class private "number"
  2. And constructor parameter "number"

Using this->number, you let the compiler know you're assigning to the class-private variable.

查看更多
闹够了就滚
5楼-- · 2020-05-25 08:20

For clarity, or to resolve ambiguity when a local variable or parameter has the same name as a member variable.

查看更多
狗以群分
6楼-- · 2020-05-25 08:21

Two main uses:

  1. To pass *this or this as a parameter to other, non-class methods.

    void do_something_to_a_foo(Foo *foo_instance);
    
    void Foo::DoSomething()
    {
        do_something_to_a_foo(this);
    }
    
  2. To allow you to remove ambiguities between member variables and function parameters. This is common in constructors.
    MessageBox::MessageBox(const string& message)
    {
      this->message = message;
    }
    (Although an initialization list is usually preferable to assignment in this particular example.)
查看更多
▲ chillily
7楼-- · 2020-05-25 08:22

Consider the case when a parameter has the same name as a class member:

void setData(int data){
  this->data = data;
}
查看更多
登录 后发表回答