Signal 调用类成员函数
将信号处理函数声明为静态成员
思想:
extern "C"
{
#include <signal.h>
#include <unistd.h>
}
#include <string>
#include <iostream>
class Test
{
public:
Test();
static void SignalProcess(int iSigNum);
};
Test::Test()
{
signal(SIGINT, &SignalProcess);
}
void Test::SignalProcess(int iSigNum)
{
std::cout << "Captured signal" << iSigNum << std::endl;
exit(0);
}
int main()
{
Test oTest;
while (true)
{
sleep(10);
}
return 0;
}
extern "C"
{
#include <signal.h>
#include <unistd.h>
}
#include <string>
#include <iostream>
extern "C" typedef void (*CFunc)(int);
typedef void (*CPPFunc)(int);
class Test
{
public:
Test();
inline void MySignal(int iSig, CPPFunc pFunc)
{
::signal(iSig, (CFunc)pFunc);
}
static void SignalProcess(int iSigNum);
};
Test::Test()
{
MySignal(SIGINT, &SignalProcess);
}
void Test::SignalProcess(int iSigNum)
{
std::cout << "Captured signal" << iSigNum << std::endl;
exit(0);
}
int main()
{
Test oTest;
while (true)
{
sleep(10);
}
return 0;
}
|