将合格的非静态成员函数作为函数指针传递 [英] Passing a qualified non-static member function as a function pointer

查看:210
本文介绍了将合格的非静态成员函数作为函数指针传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个外部库中的函数,我不能更改以下签名:

  void registerResizeCallback )(int,int))

我想传入一个成员函数作为回调,回调需要修改实例变量。



显然,这是不可能的简单:

  registerResizeCallback(& Window :: Resize); 

所以我不知道如何解决这个问题。

解决方案

检查[33.2]如何将指向成员函数的指针传递到信号处理程序,X事件回调,启动线程/任务的系统调用等? C ++常见问题解答Lite:


不要。





如果没有对象可以直接调用它,则无法直接执行。



<对于现有软件的补丁,使用顶层(非成员)函数作为包装器,它接受通过某种其他技术获得的对象。



I have a function in an external library that I cannot change with the following signature:

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

I want to pass in a member function as the callback, as my callback needs to modify instance variables.

Obviously this isn't possible with a simple:

registerResizeCallback(&Window::Resize);

so I'm not really sure how to solve the problem.

解决方案

Check "[33.2] How do I pass a pointer-to-member-function to a signal handler, X event callback, system call that starts a thread/task, etc?" at the C++ FAQ Lite:

Don't.

Because a member function is meaningless without an object to invoke it on, you can't do this directly

...

As a patch for existing software, use a top-level (non-member) function as a wrapper which takes an object obtained through some other technique.

这篇关于将合格的非静态成员函数作为函数指针传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆