C++ 从具体类派生抽象模板类
假设我有以下课程:C++ 从具体类派生抽象模板类,c++,templates,inheritance,interface,abstract-class,C++,Templates,Inheritance,Interface,Abstract Class,假设我有以下课程: template <typename T> class CModule{ public: virtual void process( std::multiamp<int, T>) = 0; } 模板 类模块{ 公众: 虚空过程(std::multiamp)=0; } 和派生类: template <typename T> class CModuleDeriv: public CModule<T>{ public:
template <typename T>
class CModule{
public:
virtual void process( std::multiamp<int, T>) = 0;
}
模板
类模块{
公众:
虚空过程(std::multiamp)=0;
}
和派生类:
template <typename T>
class CModuleDeriv: public CModule<T>{
public:
virtual void process( std::multiamp<int, T>){....};
}
模板
类别CModuleDeriv:公共CModule{
公众:
虚空过程(std::multiamp){…};
}
以及我不想实现此功能的类:
class Client{
std::vector<CModule<T>*> oModuleList_; // <--- this is not possible error
public:
void moduleLoader(){
oModuleList_.resize(1);
if( some_condition ){
oModuleList_[0] = CModuleDeriv<int>();
}else{
oModuleList_[0] = CModuleDeriv<double>();
}
}
}
类客户端{
std::vector oModuleList_;//首先,将来请发布几乎可以编译的代码(或者更好的编译代码!),因为您忽略的那些“小”细节有时会改变代码的语义。因此,提交几乎可以编译的代码几乎具有明确的语义
以下是一些可能解决您的问题的代码:
#include<map>
#include<vector>
template <class, class>
class TypedMultimap;
class TypeErasedMultimap {
public:
template <class T1, class T2>
std::multimap<T1,T2> & getMap() {
return static_cast<TypedMultimap<T1,T2> &> (*this); // This is not type safe, it can be made to be, but boost people already did it so I won't bother
}
virtual ~TypeErasedMultimap(){}
};
template <class T1, class T2>
class TypedMultimap: public TypeErasedMultimap, public std::multimap<T1,T2> {};
class CModule{
virtual void process( TypeErasedMultimap &) = 0;
};
template <typename T>
class CModuleDeriv: public CModule{
// At his point, please ask yourself, how will you make sure that the right kind
// of map arrives at this call? I can't answer this, since this is related to
// the semantics...
virtual void process( TypeErasedMultimap & map_){
std::multimap<int,T> &map = map_.getMap<int,T>();
//...
};
};
class Client{
// Why are you using raw pointers?!?
std::vector<CModule*> oModuleList_;
public:
void moduleLoader(){
oModuleList_.resize(1);
if( 1/*some condition*/ ){
oModuleList_[0] = new CModuleDeriv<int>();
}else{
oModuleList_[0] = new CModuleDeriv<double>(); // the types are lost forever...
}
}
// ~Client() you MAY OR MAY NOT!!! need a destructor since your vector is holding pointers: use smart pointers!!!
};
int main() {
}
#包括
#包括
模板
类类型多重映射;
类TypeReasedMultiMap{
公众:
模板
std::multimap&getMap(){
return static_cast(*this);//这不是类型安全的,可以将其设置为,但boost人员已经这样做了,所以我不想麻烦了
}
虚拟~type橡皮擦多重映射(){}
};
模板
类TypedMultimap:publicTypeReasedMultiMap,publicStd::multimap{};
类模块{
虚拟无效进程(TypeReasedMultiMap&)=0;
};
模板
类别CModuleDeriv:公共CModule{
//在这一点上,请扪心自问,你将如何确保正确的类型
//地图的人接到这个电话了吗?我不能回答这个,因为这和
//语义。。。
虚拟无效进程(类型擦除多映射和映射){
std::multimap&map=map_uz.getMap();
//...
};
};
类客户端{
//你为什么用原始指针?!?
std::vector oModuleList;
公众:
void moduleLoader(){
oModuleList u2; resize(1);
如果(1/*某些条件*/){
oModuleList_u0]=新的CModuleDeriv();
}否则{
oModuleList_U0]=new CModuleDeriv();//类型将永远丢失。。。
}
}
//~Client()你可以也可以不!!!需要一个析构函数,因为你的向量包含指针:使用智能指针!!!
};
int main(){
}
这本身并不能解决您的问题,因为您不编译代码的snipplet即使已编译也无法解决任何问题。您正在将具有固有不同类型的内容推到一个公共列表中,并丢失它们的类型,因此您将来如何知道如何使用这些元素?这是一个语义问题
我猜这可能就是你想要做的:
#include<boost/variant.hpp>
#include<boost/shared_ptr.hpp>
#include<boost/make_shared.hpp>
#include<map>
#include<vector>
typedef boost::variant<std::multimap<int,int>, std::multimap<int,double> /* possibly some others */ > VariantMap;
class CModule{
public:
virtual void process( VariantMap &) = 0;
};
class CModuleDeriv1: public CModule, public boost::static_visitor<> {
public:
virtual void process( VariantMap & in){
boost::apply_visitor(*this, in);
};
template < class T>
void operator()(std::multimap<int,T> & in) {
// do your type safe processing here
}
};
class CModuleDeriv2: public CModule, public boost::static_visitor<>{
public:
virtual void process( VariantMap & in){
boost::apply_visitor(*this, in);
};
template < class T>
void operator()(std::multimap<int,T> & in) {
// do other kind of processing here
}
};
class Client{
// Why are you using raw pointers?!?
std::vector<boost::shared_ptr<CModule> > oModuleList_;
public:
void moduleLoader(){
oModuleList_.resize(1);
if( 1/*some condition*/ ){
oModuleList_[0] = boost::make_shared<CModuleDeriv1>();
}else{
oModuleList_[0] = boost::make_shared<CModuleDeriv1>(); // the types are safe now, even though not known
}
}
// ~Client() you MAY OR MAY NOT!!! need a destructor since your vector is holding pointers: use smart pointers!!!
};
int main() {
}
#包括
#包括
#包括
#包括
#包括
typedef boost::变量变量映射;
类模块{
公众:
虚拟作废流程(VariantMap&)=0;
};
类CModuleDeriv1:公共CModule,公共boost::static\u visitor{
公众:
虚拟作废流程(VariantMap&in){
boost::apply_visitor(*这个,在中);
};
模板
void运算符()(std::multimap&in){
//在这里进行类型安全处理
}
};
类CModuleDeriv2:公共CModule,公共boost::static\u visitor{
公众:
虚拟作废流程(VariantMap&in){
boost::apply_visitor(*这个,在中);
};
模板
void运算符()(std::multimap&in){
//在这里做其他类型的处理
}
};
类客户端{
//你为什么用原始指针?!?
std::vector oModuleList;
公众:
void moduleLoader(){
oModuleList u2; resize(1);
如果(1/*某些条件*/){
oModuleList_u0]=boost::make_shared();
}否则{
oModuleList_u[0]=boost::make_shared();//这些类型现在是安全的,尽管还不知道
}
}
//~Client()你可以也可以不!!!需要一个析构函数,因为你的向量包含指针:使用智能指针!!!
};
int main(){
}
请参阅,不再评论唠叨:)请检查对我答案的编辑