仮想関数を持つ基本クラスがあります。
class Base
{
public:
virtual void Function();
};
void Base::Function()
{
cout << "default version" << endl;
}
および派生テンプレート クラス:
template <class T> class Derived : public Base
{
public:
virtual void Function();
};
Function()
一部の選択されたものを除いて、すべてのタイプの基本クラスから取得する方法はありますか? Function()
したがって、私が望むのは、たとえばint
andのオーバーライドを定義できるようにすることですlong
。
void Derived<int>::Function()
{
cout << "overriden version 1" << endl;
}
void Derived<long>::Function()
{
cout << "overriden version 2" << endl;
}
Function()
を明示的に定義せずに、他のすべてのタイプのデフォルトバージョンをFunction()
使用するため、の出力
int main ()
{
Derived<int> derivedInt;
derivedInt.Function();
Derived<long> derivedLong;
derivedLong.Function();
Derived<double> derivedDouble;
derivedDouble.Function();
}
だろう
overriden version 1
overriden version 2
default version
出来ますか?