[C++-sig] Function Overload Problems

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Thu Nov 14 22:04:47 CET 2002


--- "Scott A. Smith" <ssmith at magnet.fsu.edu> wrote:
> Try as I might, I still cannot get function overloading to work.
> Here is an example that I hope someone can correct:
> 
> struct X {
>     std::string Xstr;
> 
>     X() {}
> 
>      void myname(const std::string& N)
>        { Xstr = N; }
> 
>      std::string myname(double xx) const
>         { xx += 3.0; return Xstr; }
> 
>      std::string myname() const
>        { return Xstr; }
> 
> };
> 
> BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(Xname,         myname, 0, 1)
> 
> BOOST_PYTHON_MODULE(MyModule)
> {
>     class_<X>("X")
>         .def("name", (       void(X::*) (const std::string&)      )0,
> Xname())
>         .def("name", (std::string(X::*)             (double) const)0,
> Xname())
>         .def("name", (std::string(X::*)                   () const)0,
> Xname())
>         ;
> }
> 
> The first two work fine,

At face value, but you are .def'ing the overload with 0 arguments twice.

> the last one does not.

Because you promise to the macro that it will be used with a function taking up
to 1 arguments, but you are giving it one which takes no arguments.
What you want is most likely similar to:

    class_<X>("X")
        // one overload directly
        .def("name", (       void(X::*) (const std::string&)      ) &x::myname)
        // two overloads by way of using the macro
        .def("name", (std::string(X::*)             (double) const)0,
Xname())
        ;

Ralf


__________________________________________________
Do you Yahoo!?
Yahoo! Web Hosting - Let the expert host your site
http://webhosting.yahoo.com




More information about the Cplusplus-sig mailing list