[C++-sig] trouble with virtual methods

Hugo van der Merwe s13361562 at bach.sun.ac.za
Wed Jan 30 11:06:30 CET 2002


I cannot track down what I'm doing wrong. Here follows code extracts
from what I'm struggling with. If anyone can point me in the direction I
should investigate, that would be great.

Terrain.h contains:
------------
class TextureFactory
{
public:
  virtual GLuint GetTexture(int index,float originX,float originY,float width,float height) = 0;
  virtual void UnloadTexture(int index,float originX,float originY) = 0;
};
------------

My wrapper code contains:
------------
class TextureFactory_callback : public TextureFactory {
public:
    TextureFactory_callback(PyObject* self) : /*TextureFactory(),*/ m_self(self) {
	cout << "here I am" << endl;
    }
    ~TextureFactory_callback(){ cout << "ack, I'm dead!" << endl; }

    GLuint GetTexture(int index, float originX, float originY, float width, float height) {
	return python::callback<GLuint>
	    ::call_method(m_self, "GetTexture", index, originX, originY, width, height);
    }

    void UnloadTexture(int index, float originX, float originY) {
	return python::callback<void>::call_method(m_self, "UnloadTexture",
						   index, originX, originY);
    }
private:
    PyObject* m_self;
};

void func(TextureFactory* t){ t->GetTexture(0,0,0,0,0); }

BOOST_PYTHON_MODULE_INIT(demeter)
{
    python::module_builder this_module("demeter");
    python::class_builder<TextureFactory,TextureFactory_callback>
	TextureFactory_class(this_module, "TextureFactory");
    this_module.def(func, "func");
<snip>
}
------------

Now in Python I do:

>>> import demeter
>>> class boo(demeter.TextureFactory):
...   def GetTexture(a, b, c, d, e): print "gotcha:",a,b,c,d,e
...
>>> tf = boo();
here I am
>>> demeter.func(tf)
Aborted

And so it bombs out. I have looked at examples and documentation, but
have not found the answer yet.

(Note, libs/python/doc/overriding.html has a "struct baz_callback {"
which I guess should be "struct baz_callback : baz {" ? I have 1.26.0)

Thanks,
Hugo




More information about the Cplusplus-sig mailing list