// #define による定数定義の代わりに、static constメンバ変数を使用する。 // ISO/ANSI C++に準拠だが、VC6(SP4)では相変らずコンパルエラー。egcs-2.91.57 ではOK #include <iostream> using namespace std; class foo { public: static int mul( int a, int b ); static int get_i( ); private: const static int i_ = 20; // 初期化 }; int foo::mul( int a, int b ) { return a * b; } int foo::get_i( ) { return i_; } const int foo::i_; int main() { int c = foo::mul( 2, 4 ); cout << "c = " << c << endl; int i = foo::get_i(); cout << "i = " << i << endl; return 0; } /* #define で定義するとシンボルはコンパイラには伝わらないので デバッグ時の私の効率が落ちることがある。 クラス内スコープで美しく、またシンボル情報もあるので 断然いい。 また、全てstaticメンバなので、本クラスはインスタンスを生成しなく ても使用できる。だたスコープ解決しなくてはいけないのが面倒(?) */ // end of file