Name

this keyword — Object pointer in member function

Synopsis

               primary-expr := this

The this keyword can be used only in nonstatic member functions. Its value is a pointer to the target object of a member function call. If the member function is qualified (with const or volatile), the same qualifiers apply to the type of this.

Example

struct point {
  bool operator==(const point& that) {
    returnthis->x() == that.x() && this->y() == that.y(  );
  }
  bool operator!=(const point& that) {
    return !(*this == that);
  }
  bool write(FILE* fp) {
    fwrite(static_cast<void*>(this), sizeof(*this), 1, fp);
  }
};

See Also

class, expression, Chapter 6

Get C++ In a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.