[C++] mutable
Updated:
개요
- const 함수에서 멤버 변수의 값 변경이 필요한 경우 사용
- 캐시 변수 등에 사용
예제
- 코드
#include <iostream> using namespace std; class Test { private: int i; mutable int cache; public: int GetI() const { this->cache = this->i; return this->i; } void SetI(int i) { this->i = i; } int recentGetI() const { return this->cache; } }; int main() { Test t; t.SetI(1); t.GetI(); t.SetI(2); cout << t.recentGetI() << endl; return 0; }
- 실행 결과
1