我现在用我自己研究的方法写了一个类(见下面源码),与上面提到的文章的类比较有下面一些不同:1,底层关键的实现我用了memcpy,他利用union结构(这也导致我的功能的薄弱)。2,我的类的一个对象可以添加多个函数,可以实现统一的调用,而他的类的一个对象绑定一个函数,但很灵活。(这都是可以改的)3,我的类的功能弱,遇到函数所属的类有继承,虚函数,多重继承时就不对了,只适用单一无继承的类函数;而他的类对类的各种关系特点都有考虑,都可以处理。总的说能就是他的好,所以我的类写出来不是让大家用的(当然也可以在适当时候用),而是帮助大家理解C++中的委托的实现的大概方法,因为人家写的类功能强大所以实现起来就复杂,考虑的条件就多,而我的功能简单,实现也简单,相对比较容易理解,所以我的是用来看的,哈哈。代码如下:
#include <iostream>
#include <vector>
using namespace std;
class Generic//其他类型的对象的信息都要转化为这个类的对象的信息而保存下来;
{};
//这个类只接受一个输入参数的成员函数,不过改一下模板参数,
//和下面相应的代码就可以接受其他种类的函数
template<class InputType,class RetType=void>
class Functor{public: typedef Generic* PG;//指向对象的指针类型
typedef RetType (Generic::* PGF)(InputType);//指向对象成员函数的指针类型
typedef RetType (* PF)(InputType);
template<class FUNCCLASS>//模板函数
void Add(const FUNCCLASS* a_obj,RetType (FUNCCLASS::* a_objfunc)(InputType))
{
typedef RetType (FUNCCLASS::* PFF)(InputType);
char tempmem[sizeof(FUNCCLASS*)]={NULL};//转化对象信息的中间变量
GenericAndFunc* pa_GenericAndFunc=new GenericAndFunc;//储存传入对象信息的对象
memcpy(tempmem,&a_obj,sizeof(FUNCCLASS*));
memcpy(&pa_GenericAndFunc->m_pGenericObj,tempmem,sizeof(FUNCCLASS*));//实现对象地址的存储
memset(tempmem,NULL,sizeof(FUNCCLASS));
memcpy(tempmem,&a_objfunc,sizeof(PFF));
memcpy(&pa_GenericAndFunc->m_pGenericFunc,tempmem,sizeof(PFF));//实现对象成员函数地址的存储
m_pGenericAndFunc.push_back(pa_GenericAndFunc);
} //这里可以添加一个Add重载函数来实现接受普通函数和静态成员函数;
void Add(PF a_func) { m_pFunc.push_back(a_func); }
void RunAll(InputType a_input )
{
vector<GenericAndFunc*>::iterator pi;
for (pi=m_pGenericAndFunc.begin();pi!=m_pGenericAndFunc.end();pi++)
{ //下面就是调用函数(这里指针比较多比较乱)
GenericAndFunc *pgaf=*pi;
PG g=pgaf->m_pGenericObj;
PGF pgf=pgaf->m_pGenericFunc;
(g->*pgf)(a_input);
//(((*pi)->m_pGenericObj)->*((*pi)->m_pGenericFunc))(a_input); }
vector<PF>::iterator pfi;
for (pfi=m_pFunc.begin();pfi!=m_pFunc.end();pfi++)
{ (*pfi)(a_input); }
}
}
private:
class GenericAndFunc//储存传入对象信息的类
{
public: GenericAndFunc()
{ m_pGenericFunc=NULL;//存储传入对象的地址
m_pGenericObj=NULL;//存储传入对象成员函数的地址
} PG m_pGenericObj;
PGF m_pGenericFunc;
~GenericAndFunc()
{ } //不知有没有内存泄漏,<vector>应该会自己管理内存吧?
};
vector<GenericAndFunc*> m_pGenericAndFunc;
vector<PF> m_pFunc;
class A
{
public:
void showA(char a_char)
{ cout<<"A::showA "<<a_char<<endl; }
void showAA(char a_char)
{ cout<<"A::showAA "<<a_char<<endl; }};
class B
{
public: void showB(char a_char)
{ cout<<"B::showB "<<a_char<<endl; }
static void SshowB(char a_char)
{ cout<<"B::SshowB "<<a_char<<endl; }
};
class C{public: virtual void showC(char a_char)
{ cout<<"C::virtual showC"<<a_char<<endl; }};
class D : public A{public: void showD(char a_char)
{ cout<<"D(derived from A)::showD"<<a_char<<endl; }};
class E : public A,public B{public: void showE(char a_char)
{ cout<<"E(defived from A and B)::showE"<<a_char<<endl; }};
void show(char a_char){ cout<<"show "<<a_char<<endl;}
int main(){ A a; B b; C c; D d; E e;
Functor<char> a_Functor;
a_Functor.Add(&a,&A::showA);
a_Functor.Add(&a,&A::showAA);
a_Functor.Add(&b,&B::showB);
a_Functor.Add(&c,&C::showC);
a_Functor.Add(&d,&D::showD);
//a_Functor.Add(&d,&D::showA);
a_Functor.Add(&e,&E::showE);
// a_Functor.Add(&e,&E::showA);
// a_Functor.Add(&e,&E::showB);
a_Functor.Add(show);
a_Functor.Add(B::SshowB);
a_Functor.RunAll('y');