#include <iostream>
#include <string>
using namespace std;
template <typename T>
class A
{
public:
A()
{
OverrideMe();
}
virtual ~A(){};
virtual T OverrideMe()
{
throw string("A.OverrideMe called");
}
protected:
T member;
};
class B : public A<double>
{
public:
B(){};
virtual ~B(){};
virtual double OverrideMe()
{
throw string("B.OverrideMe called");
}
};
int main()
{
try
{
B b;
}
catch(string s)
{
cout << s << endl; //this prints: A.OverrideMe called
}
return 0;
}
2 に答える
1
次の例に示すように、テンプレート基本クラスからメソッドをオーバーライドできます。
#include <iostream>
template <typename T> struct Foo
{
virtual T foo() const {
std::cout << "Foo::foo()\n";
return T();
}
};
struct Bar : Foo<double>
{
virtual double foo() const {
std::cout << "Bar::foo()\n";
return 3.14;
}
};
int main(){
Bar b;
double x = b.foo();
}
出力:
Bar::foo()
于 2013-04-19T06:27:21.000 に答える
0
あなたの型T
は宣言されているが定義されていないのではないかと疑っています。
于 2013-04-19T06:32:07.213 に答える