YC Yc.W home Systems · C++ · AI Infrastructure
C & C++ · Note

C++-const成员函数

Explore all C & C++ notes →

const成员函数 const 成员函数中,this 的类型为 const X*(X 为类类型),因此不能直接修改对象的非 static、非 mutable 成员。

用于声明可通过 const 对象调用的非 static 成员函数。

单独声明和类外定义时,都要显式写出 const。

本文修订依据:C++17 工作草案 N4659 class.this、dcl.stc、class.mfct.non-static 和 dcl.fct。本文修订按 N4659(源提交 1af12266e2ba8c5a0eb0699f09fa5bde53dd70f3)限定。

AI 修改声明: 本文由 LLM 协助修订,最近修改时间:2026-09-25 08:56(UTC+08:00)。修订仅限定 const 成员函数的 this 类型、可修改成员和声明规则。


← Previous C++ shared_ptr