Question

This question is related to my another question- boost::bind return a function object which is the argument for a function that requires pointer

Except the interface of

bridge_set_pound_var_func

is not allowed to be changed.

Also, boost::function or boost::bind do not work well with the large project.

My new code is as follows:

#include <iostream>

class myA
{
 public:

 int bridge_set_pound_var_func(int (*fp)(const char *, char *, void *), void *arg)
 {
     void * b = NULL;
     int a = fp("this is poundVar", "ths is t1", b) ;
     std::cout << "bridge_set_pound_var_func is called "<< " , a is " << a << std::endl ;
     return 0;

 }

};

class myC
{
public:
myA *myOA;
int func(const char * poundVar , char * t1, void * t2);

int myCCall()
{
    myA myAO;
    myOA = &myAO;
    std::cout << "myCCall is called " << std::endl;

    myOA->bridge_set_pound_var_func( &myC::func, (void *)this );

    return 0;

 }

};

int myC::func(const char * poundVar , char * t1, void * t2)
{
 std::cout << "myC::func is called " << std::endl;
 return 1;

}

int main()
{
  myC myCO ;
  myC *m1p = &myCO ;
  m1p->myCCall() ;

  return 0 ;
}

// EOF

The errors on Linux :

  In member function 'int myC::myCCall()':

  error: no matching function for call to 'myA::bridge_set_pound_var_func(int (myC::*)(const char*, char*, void*), void*)'

  candidates are: int myA::bridge_set_pound_var_func(int (*)(const char*, char*, void*), void*)

errors on VMS:

 In member function 'int myC::myCCall()':

 error: no matching function for call to 'myA::bridge_set_pound_var_func(int (myC::*)(const char*, char*, void*), void*)'

 candidates are: int myA::bridge_set_pound_var_func(int (*)(const char*, char*, void*), void*)
Was it helpful?

Solution

The short answer is: pointers to member functions are not pointer to functions. The former need to know about the object they are called on, the latter don't. A typical approach used is to use the usually present "user data" void* to point to a suitable base class, cast the pointer and call a corresponding virtual function. Frim there you can recover the necessary object context easily.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top