[C++-sig] type traits

Piotr Jaroszyński p.jaroszynski at gmail.com
Sat Jun 30 20:41:14 CEST 2007


On Saturday 30 of June 2007 20:09:21 Roman Yakovenko wrote:
> I guess this happens because your classes don't have any virtual function.
Yeah that was really silly of me, many thanks!

Now I have a testcase for the real problem, which lead me to writing one in 
the first place:

#include <boost/python.hpp>

namespace bp = boost::python;

struct Base
{
    virtual ~Base() { }
};

struct Derived :
    Base
{
    virtual ~Derived() { }
};

struct MoreDerived :
    Derived
{
    virtual ~MoreDerived() { }
};

const Base *
get_derived()
{
    static Base * foo(new MoreDerived());
    return foo;
}

BOOST_PYTHON_MODULE(type_magic)
{
    bp::class_<Base>("Base", bp::no_init);
    bp::register_ptr_to_python<Base *>();
    bp::class_<Derived, bp::bases<Base> >("Derived", bp::no_init);
    //bp::class_<MoreDerived, bp::bases<Derived> >("MoreDerived", 
bp::no_init);
    bp::def("get_derived", &get_derived, 
bp::return_value_policy<bp::reference_existing_object>());
}

With the code above (with commented out bp::class_<MoreDerived>...) 
get_derived gives me the Base object. When I uncomment the MoreDerived 
exposure I get the MoreDerived object. The question is whether it's possible 
to get the Derived object when the MoreDerived is not exposed. This is 
exactly the situation I have as the C++ hierarchy is considered public only 
to some level.

-- 
Best Regards,
Piotr Jaroszyński



More information about the Cplusplus-sig mailing list