0

たとえば、次のコードがあります。

template <class T>
void g(T b){
  ;
}

template <class T>
void h(T b){
   ;
}

class T{
   method X;
   method Y;
   method Z; //many methods but the implementation of the methods are different from TT
};

class TT{
   method X;
   method Y;
   method Z; //the implementation of the methods are different from T
}
void f(){

  if(a==1){
  T b;  //use type T to define b
  } else{
  TT b; //use type TT to define b
  }

 g(b);
 h(b);
 i(b);// I need to use b for many different functions.

}

また、変数 b のスコープが関数 f にある必要があります。

クラス T と TT には多くのメソッドがありますが、メソッドの実装は異なります。

助言がありますか?

4

1 に答える 1

1

テンプレートを使用して、内部f()で使用するコードを移動します。bDoStuff()

template <class Type>
void DoStuff()
{
  Type b;  //use type 'Type' to define b      
}

void f(){

  if(a==1){
    DoStuff<T>();
  } else{
    DoStuff<TT>();
  }

}
于 2013-03-19T15:34:05.170 に答える