【发布时间】:2014-03-12 01:59:50
【问题描述】:
我在这里遇到了一个奇怪的场景......我有一个对模板化基类进行多重继承的类,我想在我的最终派生类中实现一个模板化实现
模板化基础:
//simple class with a pure virtual function DoFoo()
template <typename T>
struct foo
{
virtual T DoFoo() = 0;
};
base2:
//a simple class that inherits from foo, but does not implemented DoFoo()
struct bar {};
struct base2 : public foo<bar>
{
};
最终派生类:
//a class that inherits from base2, and foo<bar2>,
//that needs an implementation of DoFoo for both bar and bar2 types.
struct bar2{};
struct final : public base2, public foo<bar2>
{
template <T>
T DoFoo(){ return T();}
};
我希望看到编译器会生成 struct final 必须实现的两个虚函数,一个用于 bar 和 bar2 的 DoFoo 版本。如果编译器能够看到,模板化函数应该是能够生成匹配两个虚函数原型的模板化函数。
但是,编译器似乎认为缺少用于 bar 和 bar2 的 DoFoo() 函数,并以 " 表示错误,因为以下虚函数是纯在 " 错误中的。
我在语法方面遗漏了什么吗?或者这是我在继承和模板方面遇到的不可能的情况?为什么?
【问题讨论】:
标签: c++ templates inheritance