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

Boost Python - Overloaded Functions with default arguments problem

本文关键字:问题 重载 函数 参数 默认 Python 提升      更新时间:2023-10-16

我有一个类,它有两个函数,它们都采用一组不同的参数,并且都有这样的默认参数:

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

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

以前做过这件事的人,或者知道如何做到这一点的人可以帮助我吗?

提前致谢

这对

我有用:

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()使用其中一个重载说明符(这似乎是你所拥有的中最大的缺失部分)。