[C++-sig] how to call base class method?

zeb zebbey at gmail.com
Sat May 7 03:51:34 CEST 2011


I have 2 class exposed to python:
class A {
public:
    virtual int get() { return 1; }
};

class B : public A {
public:
    virtual int get() { return 2; }
};

In python:
obj = B()
obj.get() # this will call B's get()

For some reason, I want to call A's get() in python, like obj->A::get() in
C++. So I have tried:
@ Directly call base class's method in python: A.get(obj) # failed.
@ make a wrap function to cast B to A:
A* cast(B* obj) {
    return (A*)obj;
}
And try to use different call policy, like manage_new_object,
reference_existing_object, return_internal_reference, but all failed.
After casting, the two object actually the same object.
>>> objB = B()
>>> objA = cast(objB)
>>> objA == objB
True

Please help me
THANK YOU

--
View this message in context: http://boost.2283326.n4.nabble.com/how-to-call-base-class-method-tp3504749p3504749.html
Sent from the Python - c++-sig mailing list archive at Nabble.com.


More information about the Cplusplus-sig mailing list