[C++-sig] Re: shared_ptr<Object>

David Abrahams dave at boost-consulting.com
Sun Oct 31 18:19:51 CET 2004

John Meinel <john at johnmeinel.com> writes:

I have lots of questions about your questions, but see ******** below
before answering.

> I found an old post on how to handle handing around shared_ptr<Object>
> instead of Object.
> http://mail.python.org/pipermail/c++-sig/2004-August/007873.html
> However, I don't know how to do it when you have a virtual object 

What's a virtual object?  An instance of a class with virtual

> that you want to allow inheritance from in Python.
> Attached is a sample version of what I'm doing. Basically, I have a
> base class which allows introspection into the type. This gets
> overridden my a virtual child, which provides some functionality. And
> then finally you have private grand-children which actually implement
> this functionality.
> So what goes wrong with this situation:
> 1) If I pass no_init to a pure-virtual object 

What is a pure-virtual object?

>     with a wrapper, I cannot
>     inherit from it in Python. because I cannot call __init__ to create
>     the C++ object. And if I don't call __init__, then when a function
>     wants my inherited class, it fails with "did not match C++ signature"
> 2) Without no_init, it will compile, but I can directly create an object
>     of that type, which will cause "unidentifiable C++ exception" when
>     you try to call the function. 

That probably means your application is actually crashing, which is
usually not an error on the part of the library.  I assume you're on
Windows.  If so, #include this file in one of your source files:


and when your application crashes you'll be able to see where it is
in the debugger.

>     I assume this is because it is calling the wrapper function
>     overload, 

I don't know what overload you're speaking of.  Do you mean

>     which tries to access an invalid member function. 

What invalid member function do you think it's trying to access?  I
suggest you look at this in the debugger.  I'm fairly certain your
assumption is wrong.

>     I can live with this, but I would have really liked the safety
>     of no_init
> 3) If I add shared_ptr<mine> to the class_<> definition, I get "too
>     many template parameters" error. 

Please show code that produces the error... otherwise I will assume
you just added it 

>     If I replace the mine_w with shared_ptr<mine> then it will allow
>     me to return a shared_ptr, but not inherit the C++ object in
>     python.

What happens that indicates to you that it will "not allow you to
inherit the C++ object" in python?  BTW, you can't "inherit a C++
object."  I think I know what you mean, but using clear and correct
terminology will help me to help you.

>     If I try to do shared_ptr<mine_w>, then I still cannot create a
>     shared_ptr<mine>.

What happens that indicates to you that "still cannot create a


Did you try using 

  implicitly_convertible<shared_ptr<mine_w>, shared_ptr<mine> >()


Did you try using 

  register_ptr_to_python<shared_ptr<mine> >()


> 4) In my base class, I have whoami() as virtual. I override it in mine
>     which works fine. But I cannot override it again, without adding a
>     re-definition of "whoami". This is probably un-avoidable, especially
>     since I need to create a default-overload.
> I also saw this post:
> http://mail.python.org/pipermail/c++-sig/2004-October/008157.html
> Which has stuff like "get_override" and "public wrapper<>". Is this a
> better way to do the python wrapping. 

Yes, if you have the latest Boost CVS state.  If not, you can't use

> It's not in the tutorial

It will be, in the upcoming release.

> and the documentation in general seems pretty weak. 

What's weak about it?

> I also didn't
> find it in the documentation.
> http://www.boost.org/libs/python/doc/v2/reference.html

That's because it's not released yet.

Dave Abrahams
Boost Consulting

More information about the Cplusplus-sig mailing list