#include
using namespace std;
inline void TextFun(void)
{
cout << "普通回调函数" << endl;
}
class TextFunor
{
public:
TextFunor()
{
cout<<"构造"<<endl;
}
~TextFunor()
{
cout<<"析构"<<endl;
}
void operator()(void) const
{
cout << "()重载函数" << endl;
}
};
void ForText(void (*bFun)(void), TextFunor cFun)
{
bFun=TextFun;
bFun();
cFun();
}
int main()
{
TextFunor cFunor;
ForText(TextFun, cFunor);
return 0;
}
ForText(TextFun, cFunor);
void ForText(void (*bFun)(void), TextFunor cFun) 问题在这里,TextFunor cFun触发的构造函数是TextFunor(const TextFunor &) 这个构造函数如果不实现,
编译器也会自动分配的。
你可以实现构造函数TextFunor(const TextFunor &)是否触发就知道了。
还有一个copy constructor,你传个类对象的引用,void ForText(void (*bFun)(void), TextFunor &cFun)解决问题