#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;
}
答案 0 :(得分: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();
}
输出:
酒吧:: foo的()
答案 1 :(得分:0)
我怀疑(突然之间)您的类型T
已声明但未定义。