加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 如何将信号处理程序注册为类方法?

发布时间:2020-12-16 09:49:27 所属栏目:百科 来源:网络整理
导读:假设我有一个类A,公共方法void f(int sig).在A的构造函数中我添加了 signal(SIGSEV,boost::bind(A::f,this,_1)); 这将返回编译错误 error : cannot convert `boost::_bi::bind_tvoid,boost::_mfi::mf1void,A,int,boost::_bi::list2boost::_bi::valueA*,boost
假设我有一个类A,公共方法void f(int sig).在A的构造函数中我添加了

signal(SIGSEV,boost::bind(&A::f,this,_1));

这将返回编译错误

error : cannot convert `boost::_bi::bind_t<void,boost::_mfi::mf1<void,A,int>,boost::_bi::list2<boost::_bi::value<A*>,boost::arg<1> > >' to `__sighandler_t {aka void (*)(int)}' for argument `2' to `void (* signal(int,__sighandler_t))(int)'

知道为什么吗?

解决方法

作为C函数,信号只能采用普通函数指针,而不是任意可调用类型.您需要一个非成员包装函数和一个全局变量来存储它,以便从信号处理程序中调用成员函数.

static A * signal_object;
extern "C" void signal_handler(int signum) {signal_object->f(signum);}

// later...
signal_object = this;
signal(SIGSEGV,signal_handler);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读