C++ 中的回调,模板成员?
以下代码不起作用,但它很好地表达了我想要做的事情。 模板结构容器存在问题,我认为它应该可以工作,因为它的大小对于任何模板参数都是已知的。
class callback {
public:
// constructs a callback to a method in the context of a given object
template<class C>
callback(C& object, void (C::*method)())
: ptr.o(object), ptr.m(method) {}
// calls the method
void operator()() {
(&ptr.o ->* ptr.m) ();
}
private:
// container for the pointer to method
template<class C>
struct {
C& o;
void (C::*m)();
} ptr;
};
有什么办法可以做到这样的事情吗? 我的意思是有一个非模板类回调来包装任何指向方法的指针?
感谢 C++ 大师!
编辑:
请参阅:
Following code does NOT work, but it expresses well what I wish to do. There is a problem with the template struct container, which I think SHOULD work because it's size is known for any template argument.
class callback {
public:
// constructs a callback to a method in the context of a given object
template<class C>
callback(C& object, void (C::*method)())
: ptr.o(object), ptr.m(method) {}
// calls the method
void operator()() {
(&ptr.o ->* ptr.m) ();
}
private:
// container for the pointer to method
template<class C>
struct {
C& o;
void (C::*m)();
} ptr;
};
Is there any way to do such a thing? I mean have a non-template class callback which wraps any pointer to method?
Thanks C++ gurus!
Edit:
Please see this:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
请参阅此
C++ 中的回调,模板成员? (2)
Please see this
Callback in C++, template member? (2)
改进OP的答案:
这会打印“ok”。
在VS2008 Express上测试。
Improving the OP's answer:
This prints "ok".
Tested on VS2008 express.
你没有说你发现了什么错误,但我发现这有效:
请注意,Ptr 需要一个构造函数,因为它有一个引用成员。
您可以不使用 struct Ptr 并拥有原始成员。
使用VS2008 Express测试。
You didn't say what errors you found, but I found that this worked:
Note that Ptr needs a constructor as it has a reference member.
You could do without struct Ptr and have the raw members.
Tested with VS2008 express.
@Barry Kelly
我尝试过这个,但它不起作用(打印“ok”)......为什么?
编辑:
正如 Neil Butterworth 提到的,多态性通过指针和引用起作用,
编辑:
使用这段代码,我收到一个错误:
现在,我不明白该错误,但如果我替换 callback& c 与 const 回调& c 和 virtual voidoperator()() 与 virtualvoidoperator()() const ,它可以工作。
@Barry Kelly
I tried this, but it does not work (print "ok")... why?
Edit:
As Neil Butterworth mentioned, polymorphism works through pointers and references,
Edit:
With this code, I get an error:
Now, I don't understand that error, but if I replace callback& c with const callback& c and virtual void operator()() with virtual void operator()() const, it works.
您需要使用多态性。 使用带有虚拟调用方法(
operator()
,如果您愿意的话)的抽象基类,以及使用正确类型签名实现虚拟方法的模板化后代。按照您现在的方式,保存类型的数据是模板化的,但用于调用方法和传递对象的代码不是模板化的。 那是行不通的; 模板类型参数需要流经构造和调用。
You need to use polymorphism. Use an abstract base class with a virtual invocation method (
operator()
if you please), with a templated descendant that implements the virtual method using the correct type signature.The way you have it now, the data holding the type is templated, but the code meant to invoke the method and pass the object isn't. That won't work; the template type parameters need to flow through both construction and invocation.
我最近实现了这个:
像这样使用它:
define:
enlistcallbacks:
trigger:
您还可以创建自己的委托并覆盖他们所做的事情。 我做了其他几个,其中一个能够确保事件触发 gui 线程中的函数而不是调用它的线程。
I recently implemented this:
use it like so:
define:
enlist callbacks:
trigger:
You can also make your own delegates and overide what they do. I made a couple of others with one being able to make sure the event triggers the function in the gui thread instead of the thread it was called.
这是一个完整的工作示例,我认为您正在尝试做的事情:
This is a complete working example that does what I think you're trying to do: