Boost Python - 具有默认参数问题的重载函数

发布于 2024-11-08 07:57:57 字数 1087 浏览 0 评论 0原文

我有一个具有两个函数的类,这两个函数都采用不同的参数集,并且都有默认参数,如下所示:

void PlaySound(const std::string &soundName, int channel = 0, bool UseStoredPath = true);

void PlaySound(FMOD::Sound* sound, int channel = 0);

我从此处的教程中找到了如何执行默认参数重载

http://www.boost.org/doc/libs/1_37_0/libs/ python/doc/v2/overloads.html

以及如何在此处采用不同参数类型进行函数重载

http://boost.2283326.n4.nabble.com/ Boost-Python-def-and-member-function-overloads-td2659648.html

我最终做了这样的事情......

BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(PlaySoundFromFile, Engine::PlaySound, 1, 3)
BOOST_PYTHON_MODULE(EngineModule)
{
    class_<Engine>("Engine")
        //Sound
        .def("PlaySound", static_cast< void(Engine::*)(std::string, int, bool)>(&Engine::PlaySound));
}

问题是我真的不知道如何同时使用它们。我想避免更改我的基类函数定义。

以前做过此操作或知道如何操作的人可以帮助我吗?

提前致谢

I have a class that has two functions, both of which take a different set of parameters and both of which have default arguments like so:

void PlaySound(const std::string &soundName, int channel = 0, bool UseStoredPath = true);

void PlaySound(FMOD::Sound* sound, int channel = 0);

I've found how to do default argument overloads from the tutorial here

http://www.boost.org/doc/libs/1_37_0/libs/python/doc/v2/overloads.html

as well as how to do function overloads taking different parameter types here

http://boost.2283326.n4.nabble.com/Boost-Python-def-and-member-function-overloads-td2659648.html

and I end up doing something like this...

BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(PlaySoundFromFile, Engine::PlaySound, 1, 3)
BOOST_PYTHON_MODULE(EngineModule)
{
    class_<Engine>("Engine")
        //Sound
        .def("PlaySound", static_cast< void(Engine::*)(std::string, int, bool)>(&Engine::PlaySound));
}

The problem is I really have no idea how to use them together at the same time. I'd like to avoid having to change my base class function definitions.

Can someone who's done this before, or knows how to do this help me out?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡写薰衣草的香 2024-11-15 07:57:57

这对我有用:

BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(
    PlaySoundFromFile, Engine::PlaySound, 1, 3)
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(
    PlaySoundFromFMOD, Engine::PlaySound, 1, 2)

BOOST_PYTHON_MODULE(EngineModule)
{
    class_<Engine>("Engine")
        .def("PlaySound", static_cast< void(Engine::*)
            (const std::string&, int, bool)>
            (&Engine::PlaySound), PlaySoundFromFile())
        .def("PlaySound", static_cast< void(Engine::*)
            (FMOD::Sound*, int)>
            (&Engine::PlaySound), PlaySoundFromFMOD())
    ;
}

诀窍是您需要告诉每个 def() 使用一个重载说明符(这似乎是您所拥有的最大的缺失部分)。

This works for me:

BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(
    PlaySoundFromFile, Engine::PlaySound, 1, 3)
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(
    PlaySoundFromFMOD, Engine::PlaySound, 1, 2)

BOOST_PYTHON_MODULE(EngineModule)
{
    class_<Engine>("Engine")
        .def("PlaySound", static_cast< void(Engine::*)
            (const std::string&, int, bool)>
            (&Engine::PlaySound), PlaySoundFromFile())
        .def("PlaySound", static_cast< void(Engine::*)
            (FMOD::Sound*, int)>
            (&Engine::PlaySound), PlaySoundFromFMOD())
    ;
}

The trick is that you need to tell each def() to use one of the overload specifiers (that seemed to be the biggest missing piece from what you had).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文