itu*_*tun 6 c++ polymorphism templates
接口:
template <class T>
class Interface{
public:
typedef T Units;
virtual T get() = 0;
};
Run Code Online (Sandbox Code Playgroud)
Implementation1:
class Implementation1: public Interface<float> {
public:
float get() {
return 0.0f;
}
};
Run Code Online (Sandbox Code Playgroud)
Implementation2:
class Implementation2: public Interface<int> {
public:
int get() {
return 0;
}
};
Run Code Online (Sandbox Code Playgroud)
容器(有错误):
class Container{
private:
Interface* floatGetter;
int n;
Timer::Units* array;
public:
Container(Interface* floatGetter, int n) {
this->floatGetter= floatGetter;
this->n = n;
array = new Timer::Units[n];
}
~Container() {
}
};
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息,我有一个模板接口和一个没有模板的接口的派生类.其他一些类接受派生类的对象,但它将对象作为接口(换句话说,依赖注入).但是这个类中的接口类型是由接口实现定义的.如何在C++中实现这个想法?
EDIT1:
例:
Interface<float> myInterface1 = new Implementation1();
Interface<int> myInterface2 = new Implementation2();
Container container1 = new Container(myInterface1, 10);
Container container2 = new Container(myInterface2, 10);
Run Code Online (Sandbox Code Playgroud)
我需要容器从其实现中理解接口模板参数.
好的,首先,在这里解释问题.需要的是一个定义虚方法的接口,用于获取带模板类型的值.由于我们想要的是一个接口,get方法必须是虚拟的.另一方面,我们希望能够返回不同的类型,所以我们想要将它变为寺庙.但是,虚拟方法不能被模板化,因为编译器不知道该方法的哪个实例要包含在vtable中.
一种解决方案是在问题中完成所做的事情,即对接口类进行逻辑化.模板类型的一个重要属性是同一类的不同实例是完全不同的类型.他们没有共同的基础,他们不能相互转换.我们根本无法Interface<Generic>在常规函数中使用指针,并调用其get()方法.请考虑这一点:接口模板类型的每个实例都有一个不同的get()方法签名.这意味着在调用该方法时,必须在堆栈上发生不同的事情.编译器如何知道调用哪个版本的get()方法(如何为函数调用准备堆栈),如果它只有一个Interface<Generic>指针.
我可以想到这个问题的两个一般解决方案.
删除所有模板mumbo-jumbo并使get()方法返回一个类型擦除的对象,例如boost :: variant或boost :: any.如果我错了,请纠正我(*),但是boost :: variant就像一个联合,它记住了哪个类型的联合被赋值,而boost :: any就像一个void*,但它记得它指向的是什么类型.此解决方案路径意味着两件事:a)返回对象的类型将在运行时解析,并且在操作这些类型时会有一些开销.b)Interface的子类必须管理这些类型擦除对象之一,使它们更复杂.
将模板mumbo-jumbo放到极端并始终在模板化上下文中引用接口对象,以便编译器在这些上下文的实例化期间生成正确的函数调用.我在下面给出了一个遵循这条路径的例子.该示例创建了一个容器,用于将不同类型的Interface <>对象保存在一起,同时启用模板化功能的应用(这通常称为"访问者"?)是否正确.请注意,在该示例中,具有不同类型参数的Interface对象实际上保存在该容器类中的不同std :: lists中,因此在运行时中,不需要解析它们的类型.
免责声明:以下是一种矫枉过正的行为......
以下是如何使用具有不同模板参数的"接口"模板类的容器.我使用std :: list来保存实例,但你可以改变它.
#include<boost/fusion/container/vector.hpp>
#include<boost/fusion/algorithm.hpp>
#include<boost/mpl/transform.hpp>
#include<boost/mpl/contains.hpp>
#include<boost/utility/enable_if.hpp>
#include<boost/type_traits/add_reference.hpp>
#include<list>
#include<algorithm>
#include <iostream>
using namespace boost;
template <class T>
class Interface{
public:
typedef T Units;
virtual T get() = 0;
};
class Implementation1: public Interface<float> {
public:
float get() {
return 0.0f;
}
};
class Implementation2: public Interface<int> {
public:
int get() {
return 5;
}
};
template<class element>
struct to_list {
typedef std::list<Interface<element> *> type;
};
template<class elementVector>
struct to_containers {
typedef typename mpl::transform<elementVector,to_list<mpl::_1> >::type type;
};
class Container{
typedef fusion::vector<int,float> AllowedTypes;
typename to_containers<AllowedTypes>::type containers;
public:
template<class type> typename enable_if<mpl::contains<AllowedTypes,type>,void>::type
/*void*/ add(Interface< type/*included in AllowedTypes*/ > & floatGetter) {
fusion::deref(fusion::find<typename to_list<type>::type >(containers))
/*<type> container*/.push_back(&floatGetter);
}
template<class functional>
void apply(functional f) {
fusion::for_each(containers,applyFunctional<functional>(f));
}
private:
template<class functional>
struct applyFunctional {
functional f;
applyFunctional(functional f): f(f){}
template<class T> void operator()(T & in) const {
std::for_each(in.begin(), in.end(),f);
}
};
};
struct printValueFunctional {
template<class element>
void operator()(Interface<element> * in) const {
std::cout<<"Hi, my value is:"<<in->get()<<"\n";
}
};
int main() {
Implementation1 impl1;
Implementation2 impl2;
Interface<float> &myInterface1 = impl1;
Interface<int> &myInterface2 = impl2;
Container container;
container.add(myInterface1);
container.add(myInterface2);
container.apply(printValueFunctional());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
Hi, my value is:5
Hi, my value is:0
Run Code Online (Sandbox Code Playgroud)
嗯,对于大多数应用来说,这真的是一个巨大的矫枉过正,但你要求它:)
如果你只想要一个可以返回不同内容的界面,你也可以考虑使用boost.variant.上面的例子对它使用的所有静态多态都是真正有价值的.
编辑:大卫指出一些重要的事情,如果你出于某种原因假设不这样做,这可能是一个陷阱.此容器并不真正符合项目插入的顺序.函数调用的顺序可能不会按项目插入的顺序发生,即假设迭代将按"随机"顺序进行.
(*)boost :: variant和boost :: any在这里讨论