简体   繁体   English

C ++函数指针类型与Linux和VMS上的候选者不兼容

[英]C++ function pointer type is not compatible with candidate on Linux and VMS

This question is related to my another question- boost::bind return a function object which is the argument for a function that requires pointer 这个问题与我的另一个问题有关-boost :: bind返回一个函数对象,该对象是需要指针的函数的参数

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. 另外, boost::functionboost::bind在大型项目中不能很好地工作。

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 : 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: 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*)

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. 使用的一种典型方法是使用通常存在的“用户数据” void*指向合适的基类,强制转换指针并调用相应的虚函数。 Frim there you can recover the necessary object context easily. 在那里,您可以轻松地恢复必要的对象上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM