Boost.Python教程:重载

版权声明:本文为博主原创文章,欢迎转载(请声明出处),私信通知即可 https://blog.csdn.net/xinqingwuji/article/details/89228364

以下说明了手动包装重载成员函数的方案。当然,相同的技术可以应用于包装重载的非成员函数。

我们这里有我们的C ++类:

struct X
{
    bool f(int a)
    {
        return true;
    }

    bool f(int a, double b)
    {
        return true;
    }

    bool f(int a, double b, char c)
    {
        return true;
    }

    int f(int a, int b, int c)
    {
        return a + b + c;
    };
};

X类有4个重载函数。我们将首先介绍一些成员函数指针变量:

bool    (X::*fx1)(int)              = &X::f;
bool    (X::*fx2)(int, double)      = &X::f;
bool    (X::*fx3)(int, double, char)= &X::f;
int     (X::*fx4)(int, int, int)    = &X::f;


有了这些,我们可以继续为Python定义和包装它:

.def("f", fx1)
.def("f", fx2)
.def("f", fx3)
.def("f", fx4)


 

猜你喜欢

转载自blog.csdn.net/xinqingwuji/article/details/89228364