我有一個基類與虛擬函數:強插功能僅適用於特定類型的模板
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
和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
它是p ossible?
寫得很好的問題。 –