[C++-sig] yet another conversion problem (was: Re: c++ to python object conversion problem)

Tim Blechmann tim at klingt.org
Sun Mar 4 18:50:04 CET 2007


ok ... now i'm facing the problem, that a class derived from a c++ class
is converted to the c++ base class, in a python callback:

the c++ classes are:
    struct simple: boost::noncopyable
    {
    };

    struct holder: boost::noncopyable
    {
        simple * s_;
        void set(simple * s)
        {
            s_ = s;
        }

        void run(void)
        {
            this->ping(s_);
        }

        virtual void ping(simple * s) = 0;
    };

    struct holder_wrapper:
        holder,
        wrapper<holder>
    {
        virtual void ping(simple * s)
        {
            this->get_override("ping")(boost::ref(s));
        }
    };

which are wrapped:
        class_<simple, boost::noncopyable>("simple");

        class_<holder_wrapper, boost::noncopyable>("holder")
            .def("set", &holder_wrapper::set)
            .def("run", &holder_wrapper::run)
            .def("ping", pure_virtual(&holder_wrapper::ping))
            ;

from python, i derive new classes and implement the virtual function
holder::ping:

class simple_d(simple):
    def __init__(self, i):
        simple.__init__(self)
        self.i = i

class holder_d(holder):
    def __init__(self):
        holder.__init__(self)
    
    def ping(self, s):
        assert(isinstance(s, simple))
        assert(isinstance(s, simple_d))
        print s.i

when i call it like this the instant s in the python code, the second
assert statement fails. the object is not handled as simple_d, but as
simple:

s = simple_d(53)
h = holder_d()

h.set(s)
h.run()

is there any way to "upcast" the instance s from simple to simple_d?


thanks again!

tim

--
tim at klingt.org    ICQ: 96771783
http://tim.klingt.org

Linux is like a wigwam: no windows, no gates, apache inside, stable.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20070304/8b680621/attachment.pgp>


More information about the Cplusplus-sig mailing list