我想用 C++ 做一个类似系统的事件处理。我曾尝试创建类似的东西;
###Library
class manager
{
public:
void init(void i) { x = i };
void fire()
{
x("test");
}
private:
void x;
};
(空格)
###Console App
void tester(char* i)
{
printf("%c", i);
}
int main()
{
manager m;
m.init(tester);
m.fire();
return 0;
}
是的,我现在认为这是完全错误的方式,但我需要类似的东西。我是新手,我不明白为什么我不能做出那样的东西。
最佳答案
您似乎在寻找函数指针。像这样:
typedef void (*EventFunc)(char*);
class manager
{
public:
void init(EventFunc callback) { x = callback };
void fire()
{
x("test");
}
private:
EventFunc x;
};
其余代码应该保持不变(除了 printf("%c", i);
应该是 printf("%s", i);
).
关于c++ - 将对象/数据从库传递到控制台应用程序(如何),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17891943/