掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
クラステンプレートのメンバ関数の動的参照について (ID:73622)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
今Visual C++でC++を勉強しています。 クラステンプレート内のメンバ関数動的参照で行き詰りました。 下記コードのエラーが取れません。 template <class Type> class Check { int a; enum Opes{ ADD, SUB, MUL, OPES }; Type fadd( Type b ) const{ return a + b; } Type fsub( Type b ) const{ return a - b; } Type fmul( Type b ) const{ return a * b; } Type ope( Opes op, Type b ) const { static Type (*opes[OPES])( Type ) = { this->fadd, this->fsub, this->fmul }; return opes[op]( b ); } public: Check( int v ): a( v ){} Type operator+( int b ) const{ return ope( ADD, b ); } Type operator-( int b ) const{ return ope( SUB, b ); } Type operator*( int b ) const{ return ope( MUL, b ); } }; int main( void ) { Check<int> a = 100; std::cout << "a + 500 = " << a + 500 << '\n'; std::cout << "a - 500 = " << a - 500 << '\n'; std::cout << "a * 500 = " << a * 500 << '\n'; return 0; } 多分、コンパイラーは、 ope関数内のopes関数ポインター配列の初期化子が特定できずエラーを出しているのでしょうが、 どのように具現化したメンバ関数を指定すれば良いのかお教えいただけないでしょうか?
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2020 Takeshi Okamoto All Rights Reserved.