1、类结构代码实现
A.h头文件
#include "B.h"
class A
{
private:
A(){};
~A(){};
fun(int q);
public:
CallBackfun();
//使用可以回调函数对象
B b;
}
A.cpp文件
#include "A.h"
fun(int q)
{
//等待执行回调触发任务
}
//外部调用函数。
CallBackfun()
{
//调用b对回调函数成员callBack(int q)
b.callBack([this](int q ){fun(q);} );
//正确
b.callBack( fun);
//报错invalid use of non-static member function ,只适用于back是非类成员函数
b.callBack(&fun);
//报错error: ISO C++ forbids taking the address of an unqualified
//or parenthesized non-static member function to form a pointer to member function.
b.callBack(&A::fun);
//报错error: no matching function for call to ‘B::callBack( void (A::*)( int))’
}
2、总结
如果back(int q)只是普通函数,可以直接调用,而在成员函数,需要考虑回调函数,是有创建线程的过程,原因是 C ++禁止使用不合格或带括号的非静态成员函数的地址形成指向成员函数的指针
,所使用[this (datatype intput ){func(input);}],进行传递函数指针,来创建线程。