简体   繁体   English

静态回调函数和非静态成员

[英]Static callback function and non-static member

I actually have a code (which I can't change) needs a static function as "callback function". 我实际上有一个代码(我无法改变)需要一个静态函数作为“回调函数”。

When I change the function to static, I can't access "this" and my class members then. 当我将函数更改为静态时,我无法访问“this”和我的类成员。 Also I can't pass "this" as parameter to function, again because I can't change the original SDK and definition files, so what can I do? 另外,我不能将“this”作为参数传递给函数,因为我无法更改原始SDK和定义文件,所以我该怎么办?

My code is in a class, one of class functions calls a function which needs a "static callback function" as parameter. 我的代码在一个类中,一个类函数调用一个需要“静态回调函数”作为参数的函数。 That "callback function" needs also to access "this" and other class members to process. 那个“回调函数”还需要访问“this”和其他类成员来处理。

What can I do? 我能做什么? I'm using QT and VS 2012 as compiler. 我正在使用QT和VS 2012作为编译器。 Thanks 谢谢

A static function is in common with a global function, u can't image that u can use "this" in a global function, and not to mention class members... The only difference is a static function has a "namespace" as known as the calss name. 静态函数与全局函数是共同的,你无法想象你可以在全局函数中使用“this”,而不是提及类成员......唯一的区别是静态函数有一个“命名空间”作为被称为calss名称。 So, no matter what are you using or which compliler it is, you cannot use "this" or class members in a static function, it's just language features. 因此,无论您使用的是什么,或者使用哪种编译器,您都不能在静态函数中使用“this”或类成员,它只是语言功能。 And you should modify your code as Mike says. 你应该像迈克所说的那样修改你的代码。

If you use modern versions of C++ (11 or older) you may use std::bind . 如果您使用现代版本的C ++(11或更早版本),您可以使用std::bind Or use boost:bind . 或者使用boost:bind

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM