0

I am programming against an external library which requires a static callback function. I declared my callback as static but then i loose access to the object properties i want to modify with this callback.

.h
static void selCallback(void* userData, SoPath* selPath);

.cpp
void MyClass::selCallback(void* userData, SoPath* selPath) {
    classProperty = 3;
}

Is there a way how can create a static callback while being able to access my current objects properties? The library i use is the openInventor library. The callback wiring up is done with the following code:

SoSelection *selNode = new SoSelection;
selNode->addSelectionCallback(MyClass::selCallback);
4

2 回答 2

1

Usually there is some place where the void* userData can be specified, either when creating the library object or when registering the callback. You can set this to a pointer to the object you want to access from the callback. Then inside the callback you cast the void* pointer back to a pointer to your class and manipulate it through that.

This way you can't use C++ smart pointers, so you'll have to take care of object lifetimes yourself.

于 2016-11-14T13:17:32.730 回答
1

The addSelectionCallback method has an optional parameter for userData pointer. There you should send the object instance, which you will receive then in your callback.

Inside the method, you can typecast to the correct object type and do the actual work with the object instance.

For example:

void MyClass::selCallback(void* userData, SoPath* selPath) {
    static_cast<MyClass *>(userData)->classProperty = 3;
}

MyClass myInstance;
selNode->addSelectionCallback(MyClass::selCallback, &myInstance);
于 2016-11-14T13:20:20.040 回答