Boost logo

Boost Users :

From: Marcin Kalicinski (kalita_at_[hidden])
Date: 2007-07-08 08:15:16


This may potentially be unrelated to Boost.Python, but how to do an
equivalent of C++ dynamic_cast in Python, having all the classes wrapped
using boost.python? See below:

// All the components inherit from Component
class Component { };
class ComponentA: public Component { };
class ComponentB: public Component { };

// Find a component by name
Component *FindComponent(const char *name);

// In C++ result of FindComponent may be dynamic_casted to appropriate type:
void f()
{
    Component *c = FindComponent("a");
    ComponentA *a = dynamic_cast<ComponentA *>(c);
}

How to do this in Python (all Component classes are wrapped)?

def f():
    c = FindComponent("a")
    # Unforunately Python sees c as an instance of Component, not as
ComponentA which it really is
    a = ???

Thanks,
Marcin


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net