[C++-sig] extending a c++ object by overriding it's virtual functions..

Kleistereimer kleistereimer at gmx.de
Tue Jun 27 04:18:02 CEST 2006


i've a question about virtual functions and boost::python.
i use it with a boost c++ extended python which is embeeded in c++.

i have a virtual function that's going to be overridden in Python and
called polymorphically from C++. (so i'm not wrapping c++ for use from

for instance i want to allow users of my software to override the
virtual void Login::OnLog(std::string) function using python.

that's what 'wrapper' and 'get_override' is used for, right?
if i would use it in python only, overriding works without these stuff too.

so i duplicated the example at

and it works fine inside python as advertised:
>>> base.f()
>>> derived.f()

but for this i wont need 'wrapper' and 'get_override'. This works for
any boost c++ extension.
now for the interesting stuff: (ak the problem)

after running the script i do:

	Base *b = new BaseWrap;
	int result = b->f();
	delete b;

now i would expect the result to be 42, but i got 0.

so how to put 'wrapper' and 'get_override' to any use?

my sollution is to obtain the PyObject* of 'derived'
and to force it manualy into m_ptr of 'wrapper' using
then 'get_override' worked as expected and i got 42.

so i need to hardcode the name of the instantiated class ('derived')
into the BaseWrap class to be able to initialize the wrapper with it if
someone instantiated a c++ object and used f()... that's ugly

but my understanding of 'virtual function that's going to be overridden
in Python' is:

write a c++ class and a corresponding derived class in python, and then
use one or more instantiations of the c++ class without creating and
maintaining python instances etc.

or did i understood something completely wrong?


More information about the Cplusplus-sig mailing list