Team LiB   Previous Section   Next Section
this keyword Object pointer in member function

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) {
    return this->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

    Team LiB   Previous Section   Next Section