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?

2
  • stackoverflow.com/questions/1485983/… Your function pointer actually has the signature void (Main::*SIMPLECALLBK)(const char*) which is not what your typedef says Commented Aug 11, 2015 at 12:00
  • You're missing a ; after the declaration of helper in your Main class. I doubt that's it though. Commented Aug 11, 2015 at 12:03

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

2 Comments

why its needs to be static ?
@user63898 non-static member functions can only be called on an object. There is no object associated with &Main::SessionHelper
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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.