S.Richmond
S.Richmond

Reputation: 11558

How do you pass a non-static function as a callback?

I'm currently working with Qt and a graphics engine and during the init of the QGLWidget instance I need to pass a few function pointers to my engine.

The function looking for callbacks is:

virtual void Buffer::CreateCustom( byte* getsize, byte* makecurrent)

Qt provides a makeCurrent function however it is neither byte* nor static.

I could write a tiny wrapper function like so:

void _stdcall MakeCurrent(void)
{
    QGLContext::makeCurrent();
}

But its only meant to be called from within an instance of GLWidget. I tried to create a class member wrapper function like so:

void _stdcall LEWidget::leMakeCurrent(void)
{
    makeCurrent();
}

But you can only provide function pointers on static member functions. If I do that I get the following error:

error C2352: 'QGLWidget::makeCurrent' : illegal call of non-static member function. A nonstatic member reference must be relative to a specific object.

Upvotes: 1

Views: 10948

Answers (2)

Puppy
Puppy

Reputation: 147054

You can't. That's what std::function exists for. You need to either change your interface to use std::function, get lucky and find some kind of void* context argument, or give up.

Upvotes: 1

JosephH
JosephH

Reputation: 8825

This is because it is impossible to tell(from the compiler's POV) which this pointer should be pass into that function when that callback is called. If you really really want to pass in a pointer, you'll have to use assembly.

Upvotes: -1

Related Questions