constメンバ関数内で呼べるメンバ関数。
class Hoge { public: void Echo() { std::cout << "echo 'Hoge'" << std::endl; } void ConstEcho() const { std::cout << "const echo 'Hoge'" << std::endl; } }; class Moke { Hoge _hoge; public: void Echo() { std::cout << "echo 'Moke'" << std::endl; } void ConstEcho() const { std::cout << "const echo 'Moke'" << std::endl; } void Do() const { // メンバ関数 Echo(); // NG ConstEcho(); // OK // メンバ変数のメンバ関数 _hoge.Echo(); // NG _hoge.ConstEcho(); // OK // ローカル変数のメンバ関数 Hoge hoge; hoge.Echo(); // OK hoge.ConstEcho(); // OK } };
自身の非constメンバ関数、自身のメンバ変数の非constメンバ関数は呼び出せない。
他は呼び出せる。ローカル変数のメンバ関数とかはOK。
constメンバ関数は、自身の変更にしか関与しない。