[C++-sig] newbie questions

David Abrahams dave at boost-consulting.com
Tue Oct 8 16:08:32 CEST 2002


Stefan Seefeld <seefeld at sympatico.ca> writes:

> Hi David,
> 
> thanks a lot for your prompt replies, they are
> extremely helpful.
> 
> I now want to wrap a method that returns a 'Bar'
> pointer (in fact, what is returned is a 'BarImpl',
> derived from 'Bar').
> 
> class Bar {};
> class BarImpl : public Bar {};
> class Foo
> {
> public:
>    BarImpl *GetBar();
> };

OK, first I need to ask some questions:

1. Are you trying to wrap an interface without changing it? In other
   words, is it OK to change the definition of Foo, Bar, and BarImpl,
   or is this wrapping job to be non-intrusive?

2. What is the semantics of GetBar()? Does it just return a pointer to
   some BarImpl, to be used as a kind of reference, or is it the
   caller expected to delete (or otherwise manage) the pointer it gets
   back?

3. If the BarImpl* is just a kind of reference, which object actually
   manages the lifetime of the BarImpl object?

> I'v declared a python class for 'Bar':
> 
> python::class_<Bar,
>                 boost::shared_ptr<Bar>,
>                 boost::noncopyable> ti("Bar", python::no_init);
> 
> 
> but I figure I need to tell boost how to treat a 'BarImpl *'
> first, i.e. how to coerce a 'BarImpl *' into a
> boost::shared_ptr<Bar>.

No, not neccessarily. Maybe it would be better to stop using
boost::shared_ptr<Bar> in your class_<> definition for the time being,
and switch to using return_value_policy<manage_new_object>() for your
factory function instead... just to avoid confusion.

> I'v come across the 'ResultConverter' stuff, but
> couldn't make much use of it so far. Any hints ?

Try answering my questions above first, and we'll see which hints are
appropriate.

-- 
           David Abrahams * Boost Consulting
dave at boost-consulting.com * http://www.boost-consulting.com





More information about the Cplusplus-sig mailing list