简体   繁体   中英

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. 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?

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.

What can I do? I'm using QT and VS 2012 as compiler. 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. 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. And you should modify your code as Mike says.

If you use modern versions of C++ (11 or older) you may use std::bind . Or use boost:bind .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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