title: How to set (*sa_handler)(int) pointer to function which is member of a class? (invalid use of non-static member function)
body: <p>I'm not C++ expert and I'm trying to create a class that handles ctrl+C pressed on terminal.</p>
  
  <p>My methods were working before I started moving them to SignalHandler class.</p>
  
  <p>It looks like I cannot set sigaction.sa_action to:</p>
  
  <pre><code> sigIntHandler.sa_handler = SomeClass-&gt;someMethod(int s);
  </code></pre>
  
  <p>like I could in C.</p>
  
  <pre><code>sigIntHandler.sa_handler = someMethod(int s);
  </code></pre>
  
  <p>Compiler says:</p>
  
  <pre><code>signals.cpp: In constructor ‘SignalHandler::SignalHandler()’:
  signals.cpp:5:39: error: invalid use of non-static member function
       registerSignalHandler(ctrlCHandler);
                                         ^
  </code></pre>
  
  <p><strong>What is wrong with my code?</strong></p>
  
  <p>I guess I have to convert my class member function to something like this: <code>(*sa_handler)(int)</code>.</p>
  
  <p>cpp file:</p>
  
  <pre><code>#include "signals.h"
  
  SignalHandler::SignalHandler() {
      isSigtermReceived = false;
      registerSignalHandler(ctrlCHandler);
  }
  
  SignalHandler::~SignalHandler() {
  }
  
  // private
  
  void SignalHandler::registerSignalHandler(void (*handler)(int)){
      struct sigaction sigIntHandler;
      sigIntHandler.sa_handler = handler;
      sigemptyset(&amp;sigIntHandler.sa_mask);
      sigIntHandler.sa_flags = 0;
      sigaction(SIGINT, &amp;sigIntHandler, NULL);
  }
  
  void SignalHandler::ctrlCHandler (int s) { 
      using namespace std;
  
      cout &lt;&lt; endl &lt;&lt; "Caught signal " &lt;&lt; s &lt;&lt; ". Exiting." &lt;&lt; endl;
      isSigtermReceived = true;
  }
  </code></pre>
  
  <p>h file:</p>
  
  <pre><code>#ifndef SIGNALS_H
  #define SIGNALS_H
  
  #include &lt;signal.h&gt; // ctrl-c
  #include &lt;iostream&gt;
  
  class SignalHandler {
      public:
          SignalHandler();
          virtual ~SignalHandler();
          bool isSigtermReceived;
          void registerSignalHandler(void (*hnd)(int));
      private:
          void ctrlCHandler(int s);
  };
  
  #endif /* SIGNALS_H */
  </code></pre>
  
  <p>And this is a struct where I'm trying to assign <strong>sa_handler</strong> field:</p>
  
  <pre><code>struct sigaction {
      void     (*sa_handler)(int);
      void     (*sa_sigaction)(int, siginfo_t *, void *);
      sigset_t   sa_mask;
      int        sa_flags;
      void     (*sa_restorer)(void);
  };
  </code></pre>
  
  <p>(<a href="http://man7.org/linux/man-pages/man2/sigaction.2.html" rel="nofollow noreferrer">http://man7.org/linux/man-pages/man2/sigaction.2.html</a>)</p>