|
Home > Archive > Unix Programming > February 2006 > Signal Handling in C++
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Signal Handling in C++
|
|
|
| I have a C++ problem. I am implementing a signal handler inside a class
SignalClass. The code looks as follows:
SignalClass is an Singleton class.
class SignalClass {
private:
int assign(int sig);
void handler (int sig);
};
void SignalClass::handler (int sig) {
cout << sig << endl;
}
int SignalClass::assign(int sig)
{
sigaction sa;
sa.sa_handler = handler; // ERROR at this line
.....
}
I am getting the compiler error as "Cannot assign
void(SignalClass::*)(int) to extern "C" void(*)(int)"
Any help in solving this problem is much appreciated.
Thanks in advance.
| |
| Paul Pluzhnikov 2006-02-14, 2:49 am |
| "san" <sandeep.gond@gmail.com> writes:
> I have a C++ problem. I am implementing a signal handler inside a class
> SignalClass.
You are unlikely to produce anything that will work 
> void SignalClass::handler (int sig) {
> cout << sig << endl;
Using functions that are not async-signal safe 
You'll have mystery crashes for as long as this code shall remain.
> int SignalClass::assign(int sig)
> {
> sigaction sa;
> sa.sa_handler = handler; // ERROR at this line
Naturally.
> I am getting the compiler error as "Cannot assign
> void(SignalClass::*)(int) to extern "C" void(*)(int)"
Did you not understand what the compiler is telling you?
You are trying to establish a non-static member function as a
signal handler. Non-static member functions receive "this" as the
first (hidden) parameter. Since the kernel is not going to supply
any hidden parameters, and since signal handlers are invoked (more
or less) directly by the kernel, the compiler is telling you that
you can't do that. You can only have static member function (or a
non-member function) as a signal handler.
You may also wish to read this thread:
http://groups.google.com/group/comp...76c5d449cb4a48f
Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
| |
| David Bariod 2006-02-17, 10:40 pm |
|
On Tue, 13 Feb 2006, san wrote:
> I have a C++ problem. I am implementing a signal handler inside a class
> SignalClass. The code looks as follows:
> SignalClass is an Singleton class.
>
> class SignalClass {
> private:
> int assign(int sig);
> void handler (int sig);
> };
>
> void SignalClass::handler (int sig) {
> cout << sig << endl;
> }
>
> int SignalClass::assign(int sig)
> {
> sigaction sa;
> sa.sa_handler = handler; // ERROR at this line
> ....
> }
>
> I am getting the compiler error as "Cannot assign
> void(SignalClass::*)(int) to extern "C" void(*)(int)"
>
You are trying to affect a "pointer to member function" to
a C function pointer variable.
You can not do that.
I suggest you to declare your handler method as static.
------------------------------
David Bariod
For email replace spam by tel
|
|
|
|
|