具有不同数据类型的纯虚拟方法 [英] Pure Virtual Methods with Different Datatypes
问题描述
我正在为容器类派生一个基类,以便可以维护一致的接口.当前看起来像这样:
I'm making a base class for my container classes to derive from so I can maintain a consistent interface. It currently looks something like this:
template <typename Datatype>
class BaseClass
{
public:
virtual Datatype Foo() = 0;
virtual Datatype Bar() = 0;
};
template <typename Datatype>
class DerivedClass: public BaseClass<Datatype>
{
public:
virtual Datatype Foo()
{
}
virtual Datatype Bar()
{
}
};
但是,对于我的某些派生类,Foo()
和Bar()
可能需要具有不同的返回类型.在基类中没有每种返回类型的模板参数的情况下,如何为派生类留出一些空间来更改此类内容?
However, with some of my derived classes, Foo()
and Bar()
may need to have different return types from each other. Without having a template parameter for each different return type in the base class, how can I give the derived classes some room for changing this sort of thing?
派生类使用的类型可能完全不同且不变.确实,除了方法名称之外,不能保证派生类具有任何共同点.
The types the derived classes use are potentially completely different and invariant. Really, the derived classes aren't guaranteed to have any sort of common ground other than the method names.
推荐答案
不可能return
不同的数据类型.唯一的方法是使方法template
受到限制,因为virtual
方法不能为模板.
It's NOT possible to return
different data types. The only way would have been to make the methods template
and that is restricted because virtual
methods can not be templates.
这篇关于具有不同数据类型的纯虚拟方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!