C++ equivalent to Java this

后端 未结 3 521
走了就别回头了
走了就别回头了 2020-12-28 15:23

In Java you can refer to the current object by doing: this.x = x. How do you do this in C++?

Assume that each of these code examples are part of a clas

3条回答
  •  再見小時候
    2020-12-28 15:55

    The C++ equivalent is this, but there are a few differences.

    This is a pointer to the object in question, not a reference; so, you must use pointer dereferencing operators before accessing fields or methods.

    (*this).method(...)
    (*this).field
    

    or, using the more popular syntax

    this->method(...)
    this->field    
    

提交回复
热议问题