[C++-sig] Virtual Methods + default arguments

Bruno da Silva de Oliveira bso at inf.ufsc.br
Wed Feb 26 03:57:18 CET 2003


Hi!

Is it possible to indicate default arguments for virtual methods, using 
the BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS macro?

I have this code:

    struct X
    {
        virtual void f(int x = 10, char c = 'a')
        {
           std::cout << x << ", " << c << std::endl;
        }
    };

    void call_f(X& x)
    {
        x.f();
    }


    BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(f_overloads, f, 0, 2)

    struct XWrap: X
    {
        XWrap(PyObject* self_): self(self_) {}
        XWrap(PyObject* self_, const X&): self(self_) {}
        PyObject* self;
   
        void f(int x = 10, char c = 'a')
        {
            call_method<void>(self, "f", x, c);
        }
   
        void default_f(int x = 10, char c = 'a')
        {
            X::f(x, c);
        }
    };

    BOOST_PYTHON_MODULE(test)
    {   
        def("call_f", &call_f);
        class_< X, XWrap >("X")       
            .def("f", &X::f, &XWrap::default_f, f_overloads())
        ;
    }

But this does not compile. Here's the first line of the output of the 
compiler:

    
D:\Programming\Libraries\boost-cvs\boost\boost/python/detail/caller.hpp(53): 
error: class "f_overloads" has no member "result_converter"

So I tried to change the class_ definition to this:
   
    class_< X, XWrap >("X")       
        .def("f", &X::f, &XWrap::default_f)
        .def("f", &X::f, f_overloads())
    ;

It compiles fine, but in python:

 >>> from test import *
 >>> x = X()
 >>> x.f(10, 'c')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
RuntimeError: unidentifiable C++ exception
 >>>

I also tried to change the second .def to:

   .def("f", &XWrap::f, f_overloads())

and:

   .def("f", &XWrap::default_f, f_overloads())

But with the same results.

So, is it possible to have virtual methods with default arguments? If 
so, how? Any pointer would be greatly appreciated.

Thanks in advance,
Nicodemus.













More information about the Cplusplus-sig mailing list