C++绑定私有函数扩展(使用模板类)

class FuncRoot()
{
public:
virturl void RunAFun()=0;
}
template <typename T>
class BindFunc:public FuncRoot
{
Publib:
typedef int (T::*MumberFuncType)();
TestB(TestA* o,MumberFuncType fn):
object_(o)
,func_(fn)
{
}

virtual void RunAFun() override
{
(object_->*func)();
}
private:
MumberFuncType func_;
T* object_;
};

class TestB
{
Publib:
TestB(){}

void RunAFun()
{
func->RunAFunc();
}

void RegistAFunc(FuncRoot* func)
{
func_=func;
}

private:
FuncRoot* func_;
};

class TestA
{
public:
TestA()
{}

void RegistFunToB(TestB* b)
{
b->RegistAFunc(this,&TestA::func);
}

private:
int func()
{
int i=100;
return i;
}
};

int main()
{
TestA a;
TestB b;

a.RegistFunToB(&b);
b.RunAFun();
return 0;
}

posted @ 2019-03-30 14:52  山石散人  阅读(190)  评论(0编辑  收藏  举报