Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Thethis pointer

      From cppreference.com
      <cpp‎ |language
       
       
      C++ language
      General topics
      Flow control
      Conditional execution statements
      Iteration statements (loops)
      Jump statements
      Functions
      Function declaration
      Lambda function expression
      inline specifier
      Dynamic exception specifications(until C++17*)
      noexcept specifier(C++11)
      Exceptions
      Namespaces
      Types
      Specifiers
      constexpr(C++11)
      consteval(C++20)
      constinit(C++20)
      Storage duration specifiers
      Initialization
      Expressions
      Alternative representations
      Literals
      Boolean -Integer -Floating-point
      Character -String -nullptr(C++11)
      User-defined(C++11)
      Utilities
      Attributes(C++11)
      Types
      typedef declaration
      Type alias declaration(C++11)
      Casts
      Memory allocation
      Classes
      Class-specific function properties
      Special member functions
      Templates
      Miscellaneous
       
       

      Contents

      [edit]Syntax

      this

      The expressionthis is aprvalueexpression whose value is the address of theimplicit object parameter (object on which the implicit object member function is being called). It can appear in the following contexts:

      1) Within the body of anyimplicit object member function, includingmember initializer list, andlambda expression body(since C++11).
      2) Within thedeclaration of any implicit object member function anywhere after the (optional) cv-qualifier sequence, including theexception specification and the trailing return type(since C++11).
      4) Withincapture list of a lambda expression.
      (since C++11)

      [edit]Explanation

      this can only associate with the innermost enclosing class of its appearance, even if the appearance is invalid in the context:

      class Outer{int a[sizeof(*this)];// Error: not inside a member functionunsignedint sz= sizeof(*this);// OK: in default member initializer void f(){int b[sizeof(*this)];// OK struct Inner{int c[sizeof(*this)];// Error: not inside a member function of Inner// “this” is not associated with Outer// even if it is inside a member function of Outer};}};

      The type ofthis in a member function of classX isX* (pointer to X). If the member function isdeclared with a cv-qualifier sequencecv, the type ofthis iscv X* (pointer to identically cv-qualified X). Since constructors and destructors cannot be declared with cv-qualifiers, the type ofthis in them is alwaysX*, even when constructing or destroying a const object.

      In class templates,this is adependent expression, and explicitthis-> may be used to force another expression to become dependent.

      template<typename T>struct B{int var;}; template<typename T>struct D: B<T>{    D(){// var = 1;    // Error: “var” was not declared in this scope        this->var=1;// OK}};

      During construction of an object, if the value of the object or any of its subobjects is accessed through a glvalue that is not obtained, directly or indirectly, from the constructor'sthis pointer, the value of the object or subobject thus obtained is unspecified. In other words, the this pointer cannot be aliased in a constructor:

      externstruct D d; struct D{    D(int a): a(a), b(d.a){}// b(a) or b(this->a) would be correctint a, b;}; D d= D(1);// because b(d.a) did not obtain a through this, d.b is now unspecified

      It is possible to executedelete this;, if the program can guarantee that the object was allocated bynew, however, this renders every pointer to the deallocated object invalid, including thethis pointer itself: afterdelete this; returns, such member function cannot refer to a member of a class (since this involves an implicit dereference ofthis) and no other member function may be called.

      This can be used in the member function of the reference-counting pointer(for example,std::shared_ptr)(since C++11) responsible for decrementing the reference count, when the last reference to the managed object goes out of scope.

      class ref{// ...void incRef(){++mnRef;}void decRef(){if(--mnRef==0) delete this;}};

      [edit]Keywords

      this

      [edit]Example

      class T{int x; void foo(){        x=6;// same as this->x = 6;        this->x=5;// explicit use of this->} void foo()const{//  x = 7; // Error: *this is constant} void foo(int x)// parameter x shadows the member with the same name{        this->x= x;// unqualified x refers to the parameter// “this->” is required for disambiguation} int y;    T(int x): x(x),// uses parameter x to initialize member x               y(this->x)// uses member x to initialize member y{}     T& operator=(const T& b){        x= b.x;return*this;// many overloaded operators return *this}};

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      CWG 760C++98whenthis is used in a nested class, it was
      unspecified whether it is associated with
      the nested class or the enclosing class
      this always associates with
      the innermost nested class,
      regardless of whether it is in
      a non-static member function
      CWG 2271C++98this could be aliased when
      constructing a non-const object
      alias is also
      prohibited in this case
      CWG 2869C++98it was unclear whetherthis could be used in a
      static member function of a non-associated class
      made clear
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/this&oldid=174592"

      [8]ページ先頭

      ©2009-2025 Movatter.jp