从具体类派生抽象模板类

时间:2012-10-04 06:56:49

标签: c++ templates inheritance interface abstract-class

我们说我有以下课程:

template <typename T>
class CModule{
public:
  virtual void process( std::multiamp<int, T>)  = 0;
 }

和派生类:

template <typename T>
class CModuleDeriv: public CModule<T>{
public:
  virtual void process( std::multiamp<int, T>){....};

 }

和我要实现此功能的课程:

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>();
    }
  }
}

有可能吗? 还有其他解决方案吗? 我无法使用boost:/

1 个答案:

答案 0 :(得分:0)

首先,将来请发布几乎编译的代码(或者更好的编译代码!),因为那些&#34;很少&#34;省略的详细信息有时会更改代码的语义。因此,提交几乎可编译的代码几乎没有明确的语义......

这里有一些代码可能会或可能不会解决您的问题:

#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() {
}

这本身并不是解决您的问题的方法,因为即使编译它,您编译的代码片段也无法解决任何问题。您正在将具有固有不同类型的内容推送到公共列表并丢失其类型,因此您将如何知道将来如何使用这些元素?这是一个语义问题。

我猜测这可能是您尝试做的事情:

#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() {
}

看,没有更多评论唠叨:)