A と B の 2 つのクラスがあります。フィールドは配列とそのサイズだけです。すべてのメソッドは正確に同じであり、唯一の違いは、すべてのメソッドで取得されるインデックス整数の解釈にあります。テンプレート化して、1 つを std::plus で、もう 1 つを std::minus でインスタンス化できるようにします。
ちょっとしたコード:
class AB {
int dim;
int *tab;
public:
// obvious ctor & dtor
void fun1( int a, int b );
void fun2( int a, int b );
void fun3( int a, int b );
}
void AB::fun1( int a, int b ) {
// operation of "interpretation" here i.e. addition or subtraction of ints into indexing int _num
// some operation on tab[_num]
}
どうやってやるの?