1

I think I have misunderstood how function pointers work. In this example:

class Helper
{
  public:

      typedef void (*SIMPLECALLBK)(const char*);
      Helper(){};
      void NotifyHelperbk(SIMPLECALLBK pCbk)
      { m_pSimpleCbk = pSbk; }
  private:
     SIMPLECALLBK m_pSimpleCbk;

}

// where i call the func
class Main
{
    public:
      Main(){};
    private:
      Helper helper
      void SessionHelper(const char* msg);

}

Main.cpp

void Main::SessionHelper(const char* msg)
{
   ....
} 

helper.NotifyHelperbk(&Main::SessionHelper);

I get the following error:

error C2664: 'Main::NotifyHelperbk' : cannot convert parameter 1 from 'void (__thiscall Main::* )(const char *)' to 'Helper::SIMPLECALLBK'
1>        There is no context in which this conversion is possible

What am I missing here?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
user63898
  • 29,839
  • 85
  • 272
  • 514
  • https://stackoverflow.com/questions/1485983/calling-c-class-methods-via-a-function-pointer Your function pointer actually has the signature `void (Main::*SIMPLECALLBK)(const char*)` which is not what your `typedef` says – Cory Kramer Aug 11 '15 at 12:00
  • You're missing a `;` after the declaration of `helper` in your `Main` class. I doubt that's it though. – sweerpotato Aug 11 '15 at 12:03

2 Answers2

3

Main::SessionHelper is a non static method. So add static to it to be able to use it as function pointer. Or use member method pointer (you will need a instance to call it).

Jarod42
  • 203,559
  • 14
  • 181
  • 302
0

if you use c++11 you can use std::bind

class Helper
{
  public:
    void NotifyHelperbk(std::function<void(char*)> func){
    /* Do your stuff */
    func("your char* here");
}

And your main :

Main.cpp

Main m;

helper.NotifyHelperbk(std::bind(&Main::SessionHelper, m, std::placeholder_1));
Waxo
  • 1,816
  • 16
  • 25