[C++-sig] automatic downcasting with V2

Daniel Paull dlp at fractaltechnologies.com
Thu Dec 19 01:17:38 CET 2002


Hi all,

I followed a thread entitled "Python + Boost Python V2 + downcasting"
from the start of November this year.  I have a similar problem and am
wondering what the final solution was and whether it has been
incorporated into boost.python yet.

To save you all running to archives, the original post from Nicolas
Lelong follows.

Cheers,

Dan

------------------ SNIP -------------------------------------------
Hello,

I'm experiencing some embedding+extending with BPLv2 - and I could not =
find any answer to this simple question :

I have, say, a c++ class 'Base' and a c++ class 'Derived':

class Base
{
public:
    virtual int do_stuff() { return 0; }
};

class Derived
{
public:
    virtual int do_more() { return 100; }
};

class ObjectServer
{
public:
    Base* getObject(unsigned id)
    {
        return m_objects[id];
    }

    Base* m_objects[12];
}

declared in my module like this:

class_<Base>("Base")
    .def("do_stuff", &Base::do_stuff);

class_<Derived, bases<Base> >("Derived")
    .def("do_more", &Derived::do_more);

class_<ObjectServer>("ObjectServer")
    .def("getObject", &ObjectServer::getObject, =
return_internal_reference<>());

everything works fine - except that I can't get python to see the _real_
=
class of the objects returned by getObject. I tried the following:

...
obj =3D objectServer.getObject(0)
print isinstance(obj, Base)
print isinstance(obj, Derived)
print issubclass(Derived, Base)
print obj.__class__

and I get, when 'obj' is really a 'Derived*'
...
1
0
1
module.Base

how can I get to know that an object is an instance of Derived ?! is =
there a builtin way in Python ? a BPL v2 way ? I even thought about =
implementing my own 'isinstance' function but I'm not familiar enough =
with boost python & python !!... Any help or advice will be _greatly_ =
appreciated :)

Thanks in advance,
Nicolas.
------------------ SNIP -------------------------------------------






More information about the Cplusplus-sig mailing list