[C++-sig] Overloads.html

David Abrahams dave at boost-consulting.com
Mon Dec 16 04:27:51 CET 2002

"William Trenker" <wtrenker at hotmail.com> writes:

> It would be really helpful if the example in overloads.html included a
> second, overloaded definition of the member function f().
> I tried adding another "f" member function as follows (notice that I
> made the number of arguments the same):
> ...
> struct X
> {
>     Y& f(int x, double y = 4.25, char const* z = "wow")
>     {
>         return inner;
>     }
>     int f(int x, int y, int z) { return x + y + z; };  //another f
> with 3 args
>     Y inner;
> };
> ...
> Of course what also needs to be shown are the modifications to the
> class_<>.def's in this extended example. 
> (If I understand the relevent discussion in the mailing list, there
> has to be a properly type-coerced class_<>.def() for each overloaded
> variant of f().)

The point of using the facilities of overloads.hpp is that they
register overloads for each of the possible actual argument arities
for f().  It would also work if f were:

     Y& f(int x)
         return inner;

     Y& f(int x, double y)
         return inner;

     Y& f(int x, double y, char const* z)
         return inner;

It doesn't provide any special abilities for dealing with the case you
illustrate above.  The tutorial should probably document how to deal
with the sort of function overloading you're talking about, but I
don't think overloads.html is the right place for it.

> Can someone post the completed example with this additional f() member
> fully implemented?

    int (X::*f2)(int, int, int) = &X::f; // disambiguate the 2nd overload

    class_<X>("X", "This is X's docstring")
        .def("f", (Y&(X::*)(int,double,char const*))0, 
                X_f_overloads(args("x", "y", "z"),
                              "f's docstring"
        .def("f", f2)

> Another useful example would be where the overloaded function also
> involves a base class; perhaps a "struct Z : public X" where Z has yet
> another version of f(), also with 3 arguments. 

There's nothing special to do here.  Just wrap X and Z in the usual

> And to cover all the
> bases, how about including an override of a member function that has
> the same signature as a member function in the base class, sort of
> like this:
> Struct Z : public X {
> 	void f(void*, char*, double);  //yet another signature
> 	int f(int x, int y, int z);    //overrides an identical signature in the base class
> };

You can just use the same trick as above to disambiguate the two

> My interest in these clarifications is practical; the FLTK GUI I am
> attempting to wrap is full of overloads and sub-classes.

Oof, that's generally considered to be a poor design.  In C++,
overloads in a derived class hide the name in the base class.

> Thanks,
> Bill
> ps: In overloads.html, the line of code reading "stryct Y {};" needs
> changing to "struct Y {};".

Thanks, fixed.

                       David Abrahams
   dave at boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution

More information about the Cplusplus-sig mailing list