简体   繁体   English

C ++-访问静态void函数上的变量

[英]C++ - access variables on static void functions

This is my callback-function (ALOG is for debugging) 这是我的回调函数(ALOG用于调试)

static void playerEventCallbackA(void *clientData, SuperpoweredAdvancedAudioPlayerEvent event, void *value) {
    ALOG("###################### CALLBACK PLAYER A.... ");
    if (event == SuperpoweredAdvancedAudioPlayerEvent_LoadSuccess) {
        ALOG("###################### CALLBACK PLAYER A.... loaded"); 
        SuperpoweredAdvancedAudioPlayer *playerA = *((SuperpoweredAdvancedAudioPlayer **)clientData);        
        playerA->setBpm(126.0f);
        playerA->setFirstBeatMs(353);
        playerA->setPosition(playerA->firstBeatMs, false, false);
    };
}

i need to set the bpm here, which i have detected on a other function in this class. 我需要在这里设置bpm,这是我在此类中的其他函数上检测到的。 How can I manage this? 我该如何处理?

You cannot, since static function has no this parameter. 您不能,因为static function没有this参数。 You can access only static members, or send object of needed type to static function. 您只能访问静态成员,或将所需类型的对象发送到静态函数。

You have either to pass an object of the class to the function and use within the function the data members of the object or to create an object of the class as a local variable of the function and access the data members of the object. 您必须将类的对象传递给函数,并在函数内使用对象的数据成员,或者将类的对象创建为函数的局部变量,然后访问对象的数据成员。

For example 例如

#include <iostream>

class A
{
private:
    float a, b, c;
public:
    A( float a, float b, float c ) : a( a ), b( b ), c( c )
    {
    }

    static void display_average( const A &a )
    {
        std::cout << ( a.a + a.b + a.c ) / 3 << std::endl;
    }
};

int main()
{
   A a( 10.10f, 20.20f, 30.30f );

   A::display_average( a );
} 

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

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