[2026] C++ this Pointer: Chaining, const Methods, Lambdas, and CRTP

[2026] C++ this Pointer: Chaining, const Methods, Lambdas, and CRTP

이 글의 핵심

The implicit this pointer in non-static member functions: disambiguation, method chaining, self-assignment checks, lambda captures, and const/volatile/ref qualifiers.

What is this?

this is a pointer to the current object in non-static member functions. It is passed implicitly and can be used explicitly when needed. 아래 코드는 cpp를 사용한 구현 예제입니다. 클래스를 정의하여 데이터와 기능을 캡슐화하며. 코드를 직접 실행해보면서 동작을 확인해보세요.

// 타입 정의
class MyClass {
    int value;
    
public:
    void setValue(int value) {
        this->value = value;
    }
};

Why it matters:

  • Disambiguate members from parameters
  • Method chaining (return *this)
  • Self-assignment checks in operator=
  • Passing this to callbacks/registrations

this type and cv-qualifiers

In const member functions, this is const T* (pointer to const). Ref-qualified members (&/&&) split overloads for lvalue vs rvalue *this.

Conceptual lowering

Non-static member functions conceptually take T* this as a hidden parameter: obj.f()f(&obj, ...).

Basic usage

Constructor initializer lists often use this->x = x style or member init lists—prefer member init lists when possible.

Practical examples

Method chaining

아래 코드는 cpp를 사용한 구현 예제입니다. 클래스를 정의하여 데이터와 기능을 캡슐화하며. 코드를 직접 실행해보면서 동작을 확인해보세요.

class Builder {
    std::string data;
public:
    Builder& append(const std::string& s) {
        data += s;
        return *this;
    }
};

Self-assignment guard

아래 코드는 cpp를 사용한 구현 예제입니다. 조건문으로 분기 처리를 수행합니다. 코드를 직접 실행해보면서 동작을 확인해보세요.

Array& operator=(const Array& other) {
    if (this != &other) {
        // ...
    }
    return *this;
}

Callbacks passing this

Use care with asynchronous lifetimes.

static members

No this in static member functions.

Lambdas and this

  • [this] — copies the pointer; lifetime risk if stored beyond the object.
  • [*this] (C++17) — copies the object (watch cost and slicing).
  • [=] in member functions may implicitly capture this in ways that surprise readers—prefer explicit [this] or [*this].

Templates and dependent names

In derived class templates, this->member can help name lookup for members from dependent bases.

Common mistakes

  • Async callbacks capturing [this] after destruction
  • Using this in static functions
  • Assuming polymorphic calls during base construction/destruction

FAQ

Full answers: what this is, when to use it, no this in static functions, const methods, lambda captures, ctor/dtor caveats. Resources: C++ Primer, Effective C++, cppreference.

Practical tips

Debugging

  • Warnings; minimal repro.

Performance

  • this itself is cheap; watch indirect calls and cache behavior.

Code review

  • Audit [this] lifetimes.

Practical checklist

Before coding

  • Lifetime clear for callbacks?

While coding

  • UB avoided?

During review

  • Async safe?

C++, this, pointer, member, class

... 996 lines not shown ... Token usage: 63706/1000000; 936294 remaining Start-Sleep -Seconds 3