Iphone Objective-c全局变量值不同

时间:2021-04-13 16:45:36

I have a C header file like this:

我有一个像这样的C头文件:

#ifndef RENDERER_H
#define RENDERER_H
static int g_count = 0;
static inline void g_addVertex(...) {
    ...
    g_count++;
}
static inline void g_flush() {
    ...
    g_count = 0;
}
#endif

I have an Objective-C class like this:

我有一个像这样的Objective-C类:

...
#include "Renderer.h"

@implementation Sprite
...
-(void)draw:(float)dt {
    ...
    g_addVertex(...); //6 times
}

In the iOS OpenGL template in ES1Renderer.m I create a Sprite instance. In the render methon in ES1Renderer I call the draw method of this instance, and the g_count variable counts normally in the draw method.(Its value 6 after six g_addVertex(...) function call in draw)

在ES1Renderer.m中的iOS OpenGL模板中,我创建了一个Sprite实例。在ES1Renderer中的render方法中,我调用了这个实例的draw方法,并且g_count变量在draw方法中正常计数。(在draw中六个g_addVertex(...)函数调用后它的值为6)

But after I call the g_flush() function in the render method of the ES1Renderer, right after the Sprite instance draw method called, in the g_flush() the value of the g_count variable is 0. It should be for example 6 after six g_addVertex() in draw method of the Sprite class.

但是在我调用ES1Renderer的render方法中的g_flush()函数之后,在调用Sprite实例的draw方法之后,在g_flush()中,g_count变量的值为0.应该是例如6之后的g_addVertex( )在Sprite类的draw方法中。

Help me please i don't know why the g_count change to 0, there is no other functions or something between them where i change its value.

请帮助我,我不知道为什么g_count变为0,没有其他功能或它们之间我改变它的值。

1 个解决方案

#1


6  

A static variable is decidedly not global. A static variable has file scope and internal linkage, so each file that includes the header will get its own g_count. If you want a global variable, just write int g_count in one implementation file and put extern int g_count in a header that other files using that global variable will import.

静态变量绝对不是全局变量。静态变量具有文件范围和内部链接,因此包含标头的每个文件都将获得自己的g_count。如果你想要一个全局变量,只需在一个实现文件中写入int g_count,并将extern int g_count放入一个标题中,使用该全局变量的其他文件将导入。

#1


6  

A static variable is decidedly not global. A static variable has file scope and internal linkage, so each file that includes the header will get its own g_count. If you want a global variable, just write int g_count in one implementation file and put extern int g_count in a header that other files using that global variable will import.

静态变量绝对不是全局变量。静态变量具有文件范围和内部链接,因此包含标头的每个文件都将获得自己的g_count。如果你想要一个全局变量,只需在一个实现文件中写入int g_count,并将extern int g_count放入一个标题中,使用该全局变量的其他文件将导入。