简体   繁体   English

如何在C ++中构建一个非静态入口点?

[英]How do you build a non static entry point in c++?

I'm trying to initialize my game engine from my main entry point, RPG.cpp. 我正在尝试从我的主要入口RPG.cpp初始化游戏引擎。 This is what it includes: 这包括:

#include "stdafx.h"
#include "Engine.h"


int _tmain(int argc, _TCHAR* argv[])
{
    Engine::Go();
    return 0;
}

Engine:Go() is a public method that starts up the game engine. Engine:Go()是启动游戏引擎的公共方法。 However, it is underlined with the following error: "Error: a nonstatic member reference must be relative to a specific object" 但是,它用以下错误强调:“错误:非静态成员引用必须相对于特定对象”

Going through my engine class and making literally everything in it static fixes the problem, but that is a problem in itself. 遍历我的引擎类并将其内的所有内容静态化可以解决问题,但这本身就是一个问题。 How do I get around this error and not use the keyword static? 如何解决此错误并且不使用关键字static?

Make an instance of Engine and call Go() on it. 创建Engine的实例,然后在其上调用Go()

Engine e;
e.Go();

static functions are basically free functions that have access to private static members of the class they belong to. static函数基本上是自由函数,可以访问它们所属类的private static成员。 By "free function" I mean that they do not belong to an instance, and basically behave like normal C functions (except for their special access privileges). “自由函数”是指它们不属于实例,并且基本上表现得像普通的C函数(除了其特殊的访问特权外)。

non- static functions must be called on an instance of the class they belong to. static函数必须在它们所属的类的实例上调用。

class Klass {
    int i;
    static int s_i;
public:
    static void static_func() {
        // called without an instance of Klass
        // s_i is available because it is static
        // i is not available here because it is non-static
        // (belongs to an instance of Klass)
    }
    void non_static_func() {
        // called on an instance of Klass
        // s_i is available here because non_static_func() is a member of Klass
        // i is also available here because non_static_func() is called on
        // an instance (k) of Klass
    }
};
void free_func() {
    // s_i and i are both unavailable here because free_func()
    // is outside of Klass
}

int main() {
    // Klass::static_func() is called without an instance,
    // but with the name qualifier Klass:

    Klass::static_func();

    // Klass::non_static_func() is called with an instance:     

    Klass k;
    k.non_static_func();

    // free_func() is called without any instance, and without a name qualifier:

    free_func();
}

You have to actually create an instance of the Engine class. 您必须实际创建Engine类的实例。

For example: 例如:

Engine e;
e.Go();

You probably want non-static member functions and... 您可能需要非静态成员函数,并且...

Engine engine;
engine.Go();

static member functions don't need to be invoked on a specific object instance, but then they won't have an implicit this pointer or non- static member variables to access - they can only access other static members of the class. static成员函数不需要在特定的对象实例上调用,但是它们将没有隐式this指针或非static成员变量来访问-它们只能访问该类的其他static成员。

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

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