如何使我的C ++类事件系统成为一个限于类型数组的模板类?

我想在类模板中使用memcpy。因此,我的模板将限于任何指向C POD(结构)和char *的链接(并且可以在其他独立类中声明当前结构)。我希望任何类都能够订阅它的函数(如果它有尊重的输入参数)来转换事件。所以我的班级现在看起来像:
class IGraphElement{
    typedef void FuncCharPtr(char*, int) ;
public:
    void Add(FuncCharPtr* f)
    {
        FuncVec.push_back(f);
    }
    void CastData(char * data, int length){
        for(size_t i = 0 ; i < FuncVec.size(); i++){
            char* dataCopy = new char[length];
            memcpy(dataCopy, data, length);
            FuncVec[i](dataCopy, length);
        }
    }
private:
    vector<FuncCharPtr*> FuncVec ;
};
一般来说,我想要两件真正的东西(我试着用伪代码解释):
template < typename GraphElementDatataStructurePtrType>
class IGraphElement{
    typedef void FuncCharPtr(GraphElementDatataStructurePtrType, int) ;  // here I want FuncCharPtr to be  of type (AnyClassThatWantsToConnectToThisGraphElement::*)(GraphElementDatataStructurePtrType, int) 

public:
    void Add(FuncCharPtr* f)
    {
        FuncVec.push_back(f);
    }

    void CastData(GraphElementDatataStructurePtrType data, int length){
        for(size_t i = 0 ; i < FuncVec.size(); i++){
            GraphElementDatataStructurePtrType dataCopy = new GraphElementDatataStructurePtrType[length];
            memcpy(dataCopy, data, length);
            FuncVec[i](dataCopy, length);
        }
    }

private:
    vector<FuncCharPtr*> FuncVec ; 
  };
我想要的是什么可能以及如何将它实现到我的班级? (对不起 - 我是一个c ++ nube =()     
已邀请:
boost :: signals库解决了您的问题。 如果您对内部工作感兴趣,可以尝试使用boost :: function和boost :: bind库来实现类似的东西。 您可以研究Modern C ++ Design以获取有关仿函数模板内部工作原理的详细信息,或者只是google&amp; ask this forum。 这是使用boost的解决方案代码草图:
void DataCastHelper (boost::funtion funcCharPtr, char * data, int length) {
   char* dataCopy = new char[length];
   memcpy(dataCopy, data, length);

   funcCharPtr(dataCopy, length);
}

class IGraphElement {
public:
    void Add (FuncCharPt* f) {
        funcVec.connect(boost::bind(&DataCastHelper, f, _1, _2));
    }
    void CastData(char * data, int length){
        funcVec(data. length);
    }

private:
    boost::signal<FuncCharPtr> funcVec;
}
传递给
IGraphElement::Add
方法的
FuncCharPt* f
参数与DataCastHelper堆叠在一起,为您提供数据处理。该信号处理仿函数迭代和调用,并将参数传递给仿函数。 问候     

要回复问题请先登录注册