掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
クラステンプレートのメンバ関数の動的参照について (ID:73623)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
すみません。先ほど提示したソースコードに該当箇所以外に不具合がありました。 正しくは下記の通りです。引き続きご回答お願いいたします。 #include <iostream> template <class Type> class Check { int a; enum Opes{ ADD, SUB, MUL, OPES }; Type fadd( const Type b ) const{ return a + b; } Type fsub( const Type b ) const{ return a - b; } Type fmul( const 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+( const Type b ) const{ return ope( ADD, b ); } Type operator-( const Type b ) const{ return ope( SUB, b ); } Type operator*( const Type 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; } # 今のところ、 # fadd、fsub、fmulを名無し空間(ファイルスコープ)として実装し、 # 問題の箇所を避けています。(^^;)
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2020 Takeshi Okamoto All Rights Reserved.