gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:29:06 25 4
gpt4 key购买 nike

这个问题与我的另一个问题有关- boost::bind return a function object which is the argument for a function that requires pointer

除了

的界面
bridge_set_pound_var_func

不允许更改。

此外,boost::functionboost::bind 不适用于大型项目。

我的新代码如下:

#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

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

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

最佳答案

简短的回答是:指向成员函数的指针不是指向函数的指针。前者需要了解调用它们的对象,而后者则不需要。使用的典型方法是使用通常存在的“用户数据”void* 指向合适的基类,转换指针并调用相应的虚函数。从那里您可以轻松地恢复必要的对象上下文。

关于C++ 函数指针类型与 Linux 和 VMS 上的候选不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12441596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com