3

想想你的基本 GLUT 程序。它们只是从一个主方法运行并包含像 `glutMouseFunc(MouseButton) 这样的回调,其中 MouseButton 是方法的名称。

我所做的是将主文件封装到一个类中,这样MouseButton就不再是一个静态函数而是有一个实例。但是这样做会给我一个编译错误:

错误 2 错误 C3867:“StartHand::MouseButton”:函数调用缺少参数列表;使用 '&StartHand::MouseButton' 创建指向成员 c:\users\angeleyes\documents\visual studio 2008\projects\capstone ver 4\starthand.cpp 388 IK Engine 的指针

由于该类非常庞大,因此无法提供代码示例。

我试过使用this->MouseButton,但这给出了同样的错误。不能为回调提供指向实例函数的指针吗?

4

6 回答 6

5

正如错误消息所说,您必须使用&StartHand::MouseButton语法来获取指向成员函数 (ptmf) 的指针;这只是语言的一部分。

使用 ptmf 时,您正在调用的函数 glutMouseFunc 在这种情况下,也必须期望获得 ptmf 作为回调,否则使用非静态 MouseButton 将不起作用。相反,一种常见的技术是让回调与用户提供的void*上下文(可以是实例指针)一起工作,但执行回调的库必须明确允许此参数。确保匹配外部库(下面的handle_mouse函数)期望的 ABI 也很重要。

由于 glut不允许用户提供上下文,因此您必须使用另一种机制:将您的对象与 glut 的当前窗口相关联。但是,它确实提供了一种获取“当前窗口”的方法,我已经使用它来将 avoid*与窗口相关联。然后你只需要创建一个蹦床来进行类型转换并调用方法。

机械:

#include <map>

int glutGetWindow() { return 0; } // make this example compile and run  ##E##

typedef std::pair<void*, void (*)(void*,int,int,int,int)> MouseCallback;
typedef std::map<int, MouseCallback> MouseCallbacks;
MouseCallbacks mouse_callbacks;
extern "C" void handle_mouse(int button, int state, int x, int y) {
  MouseCallbacks::iterator i = mouse_callbacks.find(glutGetWindow());
  if (i != mouse_callbacks.end()) { // should always be true, but possibly not
                                    // if deregistering and events arrive
    i->second.second(i->second.first, button, state, x, y);
  }
}

void set_mousefunc(
  MouseCallback::first_type obj,
  MouseCallback::second_type f
) {
  assert(obj); // preconditions
  assert(f);
  mouse_callbacks[glutGetWindow()] = MouseCallback(obj, f);
  //glutMouseFunc(handle_mouse); // uncomment in non-example  ##E##
  handle_mouse(0, 0, 0, 0); // pretend it's triggered immediately  ##E##
}

void unset_mousefunc() {
  MouseCallbacks::iterator i = mouse_callbacks.find(glutGetWindow());
  if (i != mouse_callbacks.end()) {
    mouse_callbacks.erase(i);
    //glutMouseFunc(0); // uncomment in non-example  ##E##
  }
}

例子:

#include <iostream>

struct Example {
  void MouseButton(int button, int state, int x, int y) {
    std::cout << "callback\n";
  }
  static void MouseButtonCallback(
    void* self, int button, int state, int x, int y
  ) {
    static_cast<Example*>(self)->MouseButton(button, state, x, y);
  }
};

int main() {
  Example obj;
  set_mousefunc(&obj, &Example::MouseButtonCallback);

  return 0;
}

请注意,您不再直接调用glutMouseFunc;它作为[un]set_mousefunc的一部分进行管理。


以防万一不清楚:我已经重写了这个答案,所以它应该对你有用,这样它就可以避免 C/C++ 链接问题被争论。它将按原样编译和运行(没有 glut),它应该与 glut 一起工作,只需稍作修改:注释或取消注释标记的 4 行##E##

于 2010-01-14T21:02:37.100 回答
2

不,不能将指向实例函数的指针提供给期望具有特定签名的函数指针的回调函数。他们的签名不同。它不会编译。

通常,此类 API 允许您将 void* 作为“上下文”参数传入。您在那里传递您的对象,并编写一个将上下文作为回调的包装函数。包装器将其转换回您正在使用的任何类,并调用适当的成员函数。

于 2010-01-14T21:20:24.977 回答
1

您不能用实例替换静态回调。当调用者调用您的回调时,它会在哪个实例上调用?换句话说,调用者如何传入正式的“this”参数?

解决方案是有一个静态回调存根并将实例作为参数传递,这意味着被调用者必须接受一个任意的 pvoid,该 pvoid 将在调用回调时传回。在存根中,您可以调用非静态方法:

class C {
    void f() {...}
    static void F(void* p) {
      C* pC = (C*)p;
      pC->f();
    }
  }

  C* pC = ...;
  someComponent.setCallback(&C::F, pC);
于 2010-01-14T21:04:52.463 回答
1

与每个人似乎都在说的相反,您绝对可以使用非静态成员函数作为回调方法。它需要专门为获取指向非静态成员的指针而设计的特殊语法,以及在类的特定实例上调用该函数的特殊语法。有关所需语法的讨论,请参见此处。

以下是说明其工作原理的示例代码:

#include <cstdlib>
#include <string>
#include <iostream>
#include <vector>
#include <sstream>
#include <algorithm>
using namespace std;


class Operational
{
public:
    Operational(int value) : value_(value) {};

    string FormatValue() const ;

private:
    int value_;

};

string Operational::FormatValue() const
{
    stringstream ss;
    ss << "My value is " << value_;
    return ss.str();
}

typedef string(Operational::*FormatFn)() const; // note the funky syntax

Operational make_oper(int val)
{
    return Operational(val);
}

int main()
{
    // build the list of objects with the instance callbacks we want to call
    Operational ops[] = {1, 2, 3, 5, 8, 13};
    size_t numOps = sizeof(ops)/sizeof(ops[0]);

    // now call the instance callbacks
    for( size_t i = 0; i < numOps; ++i )
    {
        // get the function pointer
        FormatFn fn = &Operational::FormatValue;    

        // get a pointer to the instance
        Operational* op = &ops[i];  

        // call the callback on the instance
        string retval = (op->*fn)();

        // display the output
        cout << "The object @ " << hex << (void*)op << " said: '" << retval << "'" << endl;
    }


    return 0;
}

当我在我的机器上运行这个程序时,它的输出是:

The object @ 0017F938 said: 'My value is 1' 
The object @ 0017F93C said: 'My value is 2' 
The object @ 0017F940 said: 'My value is 3' 
The object @ 0017F944 said: 'My value is 5' 
The object @ 0017F948 said: 'My value is 8' 
The object @ 0017F94C said: 'My value is 13'
于 2010-01-14T21:54:04.503 回答
0

以下在 c++ 中用于定义 ac 回调函数,例如在您只需要此类的单个实例时使用 glut (glutDisplayFunc, glutKeyboardFunc, glutMouseFunc ...) 时很有用:

MyClass * ptr_global_instance = NULL;

extern "C" void mouse_buttons_callback(int button, int state, int x, int y) {

    // c function call which calls your c++ class method

    ptr_global_instance->mouse_buttons_cb(button, state, x, y);
}

void MyClass::mouse_buttons_cb(int button, int state, int x, int y) {

    // this is actual body of callback - ie.  if (button == GLUT_LEFT_BUTTON) ...
    // implemented as a c++ method
}

void MyClass::setup_glut(int argc, char** argv) { // largely boilerplate glut setup

    glutInit(&argc, argv);

    // ... the usual suspects go here like glutInitWindowSize(900, 800); ...

    setupMouseButtonCallback(); // <-- custom linkage of c++ to cb

    // ... other glut setup calls here
}

void MyClass::setupMouseButtonCallback() {

    // c++ method which registers c function callback

    ::ptr_global_instance = this;
    ::glutMouseFunc(::mouse_buttons_callback);
}

在您的 MyClass 标题中,我们添加:

void mouse_buttons_cb(int button, int state, int x, int y);

void setupMouseButtonCallback();

这也适用于使用相同的逻辑流程来设置您对 glutDisplayFunc(display) 的 glut 调用

于 2013-07-07T20:39:12.697 回答
0

在这种情况下,您不能使用非静态成员函数。基本上 glutMouseFunc 期望的参数类型是

void (*)(int, int, int, int)

而您的非静态成员函数的类型是

void (StartHand::*)(int, int, int, int)

第一个问题是类型并不真正匹配。其次,为了能够调用该方法,回调必须知道您的方法属于哪个对象(即“this”指针)(这就是首先类型不同的原因)。第三,我认为您使用错误的语法来检索方法的指针。正确的语法应该是:&StartHand::MouseButton。

因此,您必须将该方法设为静态或使用其他一些静态方法来知道使用哪个 StartHand 指针来调用 MouseButton。

于 2010-01-14T21:26:11.907 回答