gpt4 book ai didi

C++ 指向重载函数的指针

转载 作者:太空狗 更新时间:2023-10-29 21:45:19 25 4
gpt4 key购买 nike

我正在尝试使用 boost::python 公开重载函数。函数原型(prototype)是:

#define FMS_lvl2_DLL_API __declspec(dllexport)
void FMS_lvl2_DLL_API write(const char *key, const char* data);
void FMS_lvl2_DLL_API write(string& key, const char* data);
void FMS_lvl2_DLL_API write(int key, const char *data);

我看过这个答案:How do I specify a pointer to an overloaded function?
这样做:

BOOST_PYTHON_MODULE(python_bridge)
{
class_<FMS_logic::logical_file, boost::noncopyable>("logical_file")
.def("write", static_cast<void (*)(const char *, const char *)>( &FMS_logic::logical_file::write))
;
}

结果出现以下错误:

error C2440: 'static_cast' : cannot convert from 'overloaded-function' to 'void (__cdecl *)(const char *,const char *)'
None of the functions with this name in scope match the target type

尝试以下操作:

void (*f)(const char *, const char *) = &FMS_logic::logical_file::write;

结果:

error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'void (__cdecl *)(const char *,const char *)'
None of the functions with this name in scope match the target type

出了什么问题以及如何解决?

编辑我忘了提一些事情:

  • 我在 win-7 上使用 vs2010 pro
  • write是logical_file的成员函数
  • FMS_logic 是一个命名空间

最佳答案

如果 write 是一个纯函数,那么第二次尝试应该可以。从您的代码看来您确实有一个成员函数。指向成员函数的指针很难看,您宁愿使用函数对象。但是:您必须发布整个代码,不清楚 write 是否是成员函数。

编辑:如果它是 FMS_logic::logical_file 的成员函数,则语法为:

void (FMS_logic::logical_file::*f)(const char *, const char *) = &FMS_logic::logical_file::write;

这仅适用于非静态成员函数,即,如果函数是静态的或 logical_file 只是一个 namespace ,它就像您之前编写的那样。

关于C++ 指向重载函数的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17852738/

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