ブックマーク / frog.raindrop.jp (1)

  • クラスのメンバ変数をstatic, const, あるいはその両方で宣言する | frog.raindrop.jp.knowledge

    staticメンバ変数 クラスのメンバ変数で、値をオブジェクトごとに持つのではなく、すべてのオブジェクトで同じ変数を参照したい場合は、staticをつけて宣言する。 // sample.h class Sample { public: Sample (); ~Sample (); private: static int _staticVar; }; staticメンバ変数の初期化は、下のように実装ファイルで定義をかく。(処理系によって若干解釈が違うといううわさも) // sample.cpp #include "sample.h" int Sample::_staticVar = 0; // 初期化 constメンバ変数 クラスのメンバ変数の値を、オブジェクト作成後に変更できなくするにはconstをつけて宣言する。 // sample.h class Sample { public: Sa

    monkeydive
    monkeydive 2010/02/17
    static const
  • 1