繁体   English   中英

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

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

我正在尝试从我的主要入口RPG.cpp初始化游戏引擎。 这包括:

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


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

Engine:Go()是启动游戏引擎的公共方法。 但是,它用以下错误强调:“错误:非静态成员引用必须相对于特定对象”

遍历我的引擎类并将其内的所有内容静态化可以解决问题,但这本身就是一个问题。 如何解决此错误并且不使用关键字static?

创建Engine的实例,然后在其上调用Go()

Engine e;
e.Go();

static函数基本上是自由函数,可以访问它们所属类的private static成员。 “自由函数”是指它们不属于实例,并且基本上表现得像普通的C函数(除了其特殊的访问特权外)。

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();
}

您必须实际创建Engine类的实例。

例如:

Engine e;
e.Go();

您可能需要非静态成员函数,并且...

Engine engine;
engine.Go();

static成员函数不需要在特定的对象实例上调用,但是它们将没有隐式this指针或非static成员变量来访问-它们只能访问该类的其他static成员。

暂无
暂无

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

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