c ++指向重载函数的指针 [英] c++ pointers to overloaded functions

查看:295
本文介绍了c ++指向重载函数的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图暴露一个重载函数使用boost :: python。
函数原型是:

I'm trying to expose a overloaded function using boost::python. the function prototypes are:

#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);

我看到这个答案:如何指定一个重载函数的指针?

执行此操作:

I'v seen this answer: how to specify a pointer to an overloaded function?
doing this:

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

有什么问题以及如何解决?

what's wrong and how to fix it?

EDIT
我忘记了几件事情:

EDIT I forgotten to mention a few things:


  • m使用vs2010 pro on win-7

  • write是logical_file的成员函数

  • FMS_logic是命名空间

推荐答案

第二个attemp应该工作,如果写是一个纯函数。从你的代码,似乎你有一个成员函数。指向成员函数的指针是丑陋的,你宁愿使用一个函数对象。
然而,你必须发布整个代码,不清楚写是否是一个成员函数。

Well the second attemp should work, if write is a pure function. From your code it seems you do have a memberfunction. Pointers to member-functions are ugly, you'd rather use a function object. However: you would have to post the whole code, it is not clear whether write is a member-function or not.

编辑:如果它是一个FMS_logic :: logical_file的成员函数的语法是:

if it is a member-function of FMS_logic::logical_file the syntax would be:

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

这只适用于非静态成员函数,即如果函数是static或logical_file只是一个命名空间就是你之前写的。

This just applies for non-static member function, i.e. if a function is static or logical_file is just a namespace it is as you wrote it before.

这篇关于c ++指向重载函数的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆