简体   繁体   English

类静态函数中变量的非静态成员引用

[英]A non-static member reference of a variable inside a class' static function

I want to take the OpenGL functionality from the main source file to a separate class. 我想将OpenGL功能从主要源文件带到一个单独的类。

// Initialize rendering (GLUT and GLEW)
gfxMgr.init(argc, argv);
...
glutReshapeFunc(gfxMgr.resizeWindow);
glutKeyboardFunc(gfxMgr.keyPressed);
glutKeyboardUpFunc(gfxMgr.keyReleased);

The problem started with defining the callback functions within the class' implementation file. 问题始于在类的实现文件中定义回调函数。 I declared them in the header file as static. 我在头文件中将它们声明为静态。

static void init(int , char** );
...
static void drawScene();
static void whenIdle();

Then another problem followed. 然后是另一个问题。 I want to use a non-static boolean fullScreen variable (as declared in the header) in one of the static functions of my implementation file, but the IDE tells me that "a non-static member reference must be relative to a specific object". 我想在实现文件的一个静态函数中使用一个非静态的boolean fullScreen变量(如标头中所声明),但是IDE告诉我“一个非静态成员引用必须相对于特定对象”。 。

void GfxMgr::init(int argc, char** argv)
{
    ...
    if(fullScr) glutFullScreen();
    ...
}

I don't understand the problem and I don't know what to do. 我不明白问题所在,也不知道该怎么办。 I declared the boolean and a few other variables as static but came up with a bunch of unresolved external symbol errors. 我将布尔值和其他一些变量声明为静态变量,但提出了许多未解决的外部符号错误。

I don't understand the problem 我不明白这个问题

You need to understand what static member functions are. 您需要了解什么是静态成员函数。 See this tutorial for example. 例如,请参阅本教程 I also recommend the previous one about static member variables. 我还推荐了有关静态成员变量的上一篇。

In short, static member functions don't know anything about instances of the class. 简而言之,静态成员函数对类的实例一无所知。 Non-static member variables are bound to an instance of the class. 非静态成员变量绑定到该类的实例。 Therefore non-static member variables are unavailable to static member functions. 因此,非静态成员变量不适用于静态成员函数。

and I don't know what to do. 而且我不知道该怎么办。

What you should do depends on what your class and it's functions are supposed to do. 您应该做什么取决于您的类及其功能。

  • If the behaviour of the function is supposed to depend on value of a member variable of an instance, then the function must be non-static and you must call it on the instance. 如果功能的行为应取决于实例的成员变量的值,则该功能必须是非静态的,并且必须在实例上调用它。
  • If the function is supposed to only depend on global state of the class rather than an instance, then the variables the function accesses must be part of that global state (static members). 如果假定函数仅依赖于类的全局状态而不是实例,则函数访问的变量必须是该全局状态的一部分(静态成员)。

C callbacks cannot be member functions. C回调不能是成员函数。 Therefore they cannot depend on state of an instance (except a global instance, see this tutorial ) 因此,它们不能依赖实例的状态(全局实例除外,请参阅本教程

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

相关问题 必须在模板化类上调用对非静态成员函数的引用 - Reference to non-static member function must be called on templated class 非静态成员函数的引用 - Reference to non-static Member Function 处理类内部的回调时,必须调用对非静态成员函数的引用 - Reference to non-static member function must be called when processing callbacks inside a class 在比较函数中使用非静态类成员 - Using a non-static class member inside a comparison function 未定义引用静态成员函数内的静态类成员变量 - undefined reference to `Static Class Member variable inside Static member function' 私有成员函数中非法引用非静态成员 - Illegal Reference to Non-static Member in Private Member Function 创建指向非静态类成员函数的类成员指针函数变量 - Creating a class member pointer function variable that points to a non-static class member function 将非静态成员 function 传递给另一个 class 的成员 function - Pass non-static member function to member function of another class 结构内部结构:“非法引用非静态成员”错误 - Struct inside struct: 'Illegal reference to non-static member' error 为什么非静态数据成员引用不是变量? - Why is a non-static data member reference not a variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM