是否可以在C ++类中使用信号? [英] Is it possible to use signal inside a C++ class?

查看:116
本文介绍了是否可以在C ++类中使用信号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在这样做:

#include <signal.h>

class myClass {

    public: 

    void myFunction () 
    {
    signal(SIGIO,myHandler);
    }

    void myHandler (int signum)
    {
    /**
    * Handling code
    */
    }

    }

我在Ubuntu上使用gcc。

I am working on Ubuntu, using gcc.

但它不会编译。它抱怨:

But it won't compile. It is complaining with:


错误:类型的参数void(MyClass ::)(int) code>不符合 void(*)(int)

任何线索?或者也许只是我不能在类里面使用信号?信号只允许在C?

Any clues? Or maybe it is just that I cannot use a signal inside classes? Are signals only allowed in C?

错误讯息是一个大致的翻译,因为我的编译器不是英文。

The error message is an approximate translation because my compiler is not in English.

推荐答案

signal的第二个参数应该是一个指向接受int并返回void的函数的指针。传递的信号是指向成员函数的指针,接受一个int并返回void(其类型为 void(myClass :: *)(int))。我可以看到三种可能性来克服这个问题:

The second parameter of signal should be a pointer to a function accepting an int and returning void. What you're passing to signal is a pointer to a member function accepting an int and returning void (its type being void (myClass::*)(int)). I can see three possibilities to overcome this issue:

1 - 您的方法 myHandler 可以是静态的: ,使其静态

1 - Your method myHandler can be static: this is great, make it static

class myClass 
{
  public:
    void myFunction () 
    {
        signal(SIGIO, myClass::myHandler);
    }

    static void myHandler (int signum)
    {
        // handling code
    }
};

2 - 您的方法不应该是静态的:如果您打算仅使用一个实例,可以创建一个私有静态对象,并写一个静态方法,只需调用该对象的方法。类似于

2 - Your method shouldn't be static: if you're planning to use signal with only one instance, you can create a private static object, and write a static method that simply call the method on this object. Something along the lines of

class myClass 
{
  public:
    void myFunction () 
    {
        signal(SIGIO, myClass::static_myHandler);
    }

    void myHandler (int signum)
    {
        // handling code
    }

    static void static_myHandler(int signum)
    {
        instance.myHandler(signum);
    }

  private:
    static myClass instance;
};

3 - 然而,如果你打算使用多个实例的信号,复杂。也许一个解决方案是将你想要操作的每个实例存储在一个静态向量中,并调用以下方法:

3 - However, if you're planning on using the signal with multiple instances, things will get more complicated. Perhaps a solution would be to store each instance you want to manipulate in a static vector, and invoking the method on each of these :

class myClass
{
  public:
    void myFunction () // registers a handler
    {
        instances.push_back(this);
    }

    void myHandler (int signum)
    {
        // handling code
    }

    static void callHandlers (int signum) // calls the handlers
    {
        std::for_each(instances.begin(), 
                      instances.end(), 
                      std::bind2nd(std::mem_fun(&myClass::myHandler), signum));
    }
  private:
    static std::vector<myClass *> instances;
};

在某处,只需调用

signal(SIGIO, myClass::callHandlers);

但我认为如果你最终使用最后一个解决方案,你应该考虑改变你的处理设计: - )!

But I think that if you end up using the last solution, you should probably think about changing your handling design :-)!

这篇关于是否可以在C ++类中使用信号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆