定义静态类函数



我有这样的c++代码:

static class UEnum* EViewModeIndex_StaticEnum()
{
    static class UEnum* Singleton = NULL;
    if (!Singleton)
    {
        ...
    }
return Singleton;
}

这段代码是游戏引擎的一部分,可以正确编译和运行。但是,我不理解第一行和第三行"class"的含义

当您有class A时,您可以使用class A来声明变量、返回类型或参数类型。这和使用A来达到同样的目的是一样的。在这种情况下,使用class是多余的,但是合法的。

class A
{
   // ...
};
void foo()
{
   // Create an instance of A using the simple syntax.
   A f1;
   // Create an instance of A using the redundant class keyword
   // Use of class is redundant but legal.
   class A f2;
}

然而,在某些情况下,有必要使用class/struct关键字来消除与class/struct同名的函数的歧义。

来自c++ 11标准:

9.1类名

类声明将类名引入声明它的作用域,并在封闭作用域中隐藏任何类、变量、函数或其他声明该名称的声明(3.3)。如果在作用域中声明了类名,并且声明了同名的变量、函数或枚举数,那么当这两个声明都在作用域中时,只能使用详细的类型说明符来引用类(3.4.4)。(例子:

struct stat {
     // ...
  };
 stat gstat;   // use plain stat to
               // define variable
 int stat(struct stat*);   // redeclare stat as function
 void f() {
   struct stat* ps;        // struct prefix needed
                           // to name struct stat
   stat(ps);               // call stat()
}

- end示例]

这是static函数。classUEnum在一起。这个静态函数的返回类型是class UEnum*。但通常,我们在这里不写class。它相当于static UEnum* EViewModeIndex_StaticEnum()

最新更新