掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
クラステンプレートのメンバ関数の動的参照について (ID:73624)
名前
ホームページ(ブログ、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 (Check::*opes[OPES])( Type ) const = { &Check::fadd, &Check::fsub, &Check::fmul }; return (this->*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; }
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2020 Takeshi Okamoto All Rights Reserved.