[C++] this pointer

this pointer

  • Every member function (including construction and destruction) has a this pointer
  • The this pointer points to the calling object, that is, the members of the current object can be accessed through the this keyword
访问成员变量: this->成员名;
访问成员函数: this->函数名();

note:

  1. The type of this pointer is class type * const (class name * const), which is an rvalue.
  2. The this pointer itself does not occupy size , it is not part of the object, so it will not affect the result of sizeof.
  3. The scope of this is inside the class member function.
  4. The this pointer is the first default hidden parameter of the class member function. The compiler automatically maintains the transmission, and the class writer cannot display the transmission.
  5. This pointer can only be used in non-static member functions of the class, not any other functions.
    Insert picture description here
函数的结尾声明为const:函数内部不允许修改对象本身,调用本对象非const方法就会报错。

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_46527915/article/details/114024349