[C++-sig] Re: Inheritance with B/P

David Abrahams dave at boost-consulting.com
Mon Feb 10 16:38:20 CET 2003


Bruce Lowery <bruce_lowery at yahoo.com> writes:

> --- David Abrahams <dave at boost-consulting.com> wrote:
>> Bruce Lowery <bruce_lowery at yahoo.com> writes:
>> 
>> >> For me it's look like a C++ problem not a Python problem.
>> >> auto_ptr<Derived> is not inherited from auto_ptr<Base> and/or
>> >> there is no automatic conversion from former to later.
>> >> 
>> >
>> > You are right.
>> 
>> That's not the problem in the sample you posted.
>> 

I said that because at no point in your code were you passing
any auto_ptrs to functions.

> And yet, this was the root of the problem - as was indicated next.
>
>
>> > Turns out I left out another detail - the method that's being called with
>> the
>> > derived class instance, is itself wrapped by a function call.  For
>> instance,
>> >
>> > class SomeOther { void someFunc( Base* b ); }
>> >
>> > void someFunc_wrap( SomeOther& self, auto_ptr<Base> b ) 
                                          ^^^^^^^^^^^^^^
>> > { self.someFunc(b.get()); }

Ah, I missed that buried detail.

>> > BOOST_PYTHON_MODULE(Foo){ class_<SomeOther>("SomeOther",...)
>> > .def( "someFunc",someFunc_wrap ); }
>> >
>> >
>> > Previously, I just had '.def("someFunc",&SomeOther::someFunc)'
>> > which works, evidently because b/p takes care of the conversion.
>> 
>> You've still left out a lot of detail.  How hard can it be to
>> assemble a reproducible case based on the code you're posting here?
>> You might discover that your problem is elsewhere (if it works),
>> which would save both of us a lot of wasted effort ;o).  Otherwise,
>> I'd be able to check it out and tell you where the problem is.
>> 
>
> There's hardly anything more to it.  Here's what I built and tested
> (the use of auto_ptr appears unnecessary in this example, but is
> needed in the real implementation):

<snip>

> Notice that to fix the problem, I overloaded the wrapper function
> (for SomeOther::someFunc) to take either auto_ptr<Derived> or
> auto_ptr<Base>.
>
> This compiles and executes successfully.
>
> However, it's not an attractive solution since I have to add a
> function wrapper for every subclass of Base that could be passed to
> SomeOther::someFunc().
>
> Is there a better way to do this?

Does 

     implicitly_convertible<
         std::auto_ptr<Derived>
       , std::auto_ptr<Base>
     >();

, placed in your module init function, help?

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com





More information about the Cplusplus-sig mailing list