语法的含义 "ContextRegistrar_##ContextType"

Meaning of the syntax "ContextRegistrar_##ContextType"

我很难理解以下 #define 的确切作用。

#define REGISTER_CONTEXT( ContextType ) static const FContextRegistrar ContextRegistrar_##ContextType( ContextType::StaticClass() );
REGISTER_CONTEXT(UBlueprintContext);

据我所知,它将 UClass 添加到数组中,以便其他函数可以使用它并进行迭代。但是

ContextRegistrar_##ContextType

在这方面做什么?谁能给我一个提示吗? 这导致我运行时崩溃,我找不到类似的东西。

这是对应的结构体:

struct FContextRegistrar
{
    static TArray<TSubclassOf<UBlueprintLibraryBase>>& GetTypes()
    {
        static TArray<TSubclassOf<UBlueprintLibraryBase>> Types;
        return Types;
    }

    FContextRegistrar( TSubclassOf<UBlueprintLibraryBase> ClassType )
    {
        GetTypes().Add( ClassType );
    }
};

这是在宏中连接标记的方法,参见Concatenation

因此,在您的情况下:您程序中的 REGISTER_CONTEXT(Bar) 将扩展为 ContextRegistrar_Bar 作为宏的一部分。

## 是 C 和 C++ 预处理器中的标记粘贴运算符。您可以使用它创建新的令牌。例如:

#define MACRO(x) x ## 1

此宏创建一个与其参数 x 相同但附加了 1 的新标记。如果你像 MACRO(1) 这样调用它,结果将是整数文字 11,MACRO(a) 的结果将是 a1,你可以将其用作变量、函数、class 等的名称

在您的示例中,REGISTER_CONTEXT(UBlueprintContext); 将生成以下代码:

static const FContextRegistrar ContextRegistrar_UBlueprintContext( UBlueprintContext::StaticClass() );