Shi*_*hah 4 c++ parameters syntax unreal-engine4
虚幻引擎生成以下功能:
void AFlyingPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
//stuff...
}
Run Code Online (Sandbox Code Playgroud)
注意参数类型之前的"class"说明符.这是什么意思?
1.第一种可能性,如果之前未宣布,可能是前瞻性声明UInputComponent.所以
void AFlyingPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
Run Code Online (Sandbox Code Playgroud)
forward声明一个名为的类UInputComponent,参数InputComponent是type UInputComponent*.
请注意,新的类名也可能由精心设计的类型说明符引入,该说明符作为另一个声明的一部分出现,但仅当名称查找无法找到先前声明的具有相同名称的类时.
Run Code Online (Sandbox Code Playgroud)class U; namespace ns{ class Y f(class T p); // declares function ns::f and declares ns::T and ns::Y class U f(); // U refers to ::U Y* p; T* q; // can use pointers and references to T and Y }
2.第二种可能性,关键字class可能用于消除歧义.
如果函数或变量存在于范围内,其名称与类类型的名称相同,则可以在类之前添加类以消除歧义,从而生成详细的类型说明符
例如
int UInputComponent;
class UInputComponent { /* ... */ };
// without the class keyword it won't compile because the name UInputComponent is ambiguous
void AFlyingPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
Run Code Online (Sandbox Code Playgroud)
3.第三种可能性,它可能没有任何意义.
如果UInputComponent已经声明为类,那么使用关键字class与否不会改变任何内容.请注意,如果先前声明的类型不匹配,则编译将失败.
您可能知道,Unreal 管理相同基类的多个实现来定义共同点。然后,每个开发人员都必须从引擎必须提供的子类中创建子类,以便在引擎内执行任务。
在本例中,它是关于 InputComponent 的,它用于处理用户输入、解释它并将其传递给控制器和/或随后的 Pawn。
例如,如果您想定义诸如Pawns、PlayerControllers、AIControllers、HUD等元素,您可以在GameMode中执行此操作,然后将其配置到项目设置中,或者直接通过世界设置配置到关卡中(以防万一)您的关卡需要特定的GameMode)。这些引用也是类,它们将由 Engine 在适当的时候实例化以设置Game。
考虑到这一点,缺点就来了。在 UE4 C++ 中(是的,这是一件事!),由于引擎解决了松散的问题,有时您将无法使用某些类,因为它们没有声明。当然,您可以包含它们,但请想一想:如果您为一个类添加了所需的所有包含项,却发现另一个类可能间接需要该类,将会创建多少个循环依赖项?
解决方案是前向声明。然而,这种情况是一种特殊的风格,称为速记前向声明,其中您在使用该类的确切位置声明一个类型。
如果您只使用一次,这将非常方便,因此您不会在文件开头出现糟糕的声明列表。
例如,如果您想知道当前定义的默认 Pawn 类,您可以检查GameModeGetDefaultPawnClass中的公共变量 (我们称之为)。该变量定义如下:MyGameMode
TSubclassOf < APawn > DefaultPawnClass
Run Code Online (Sandbox Code Playgroud)
看到了吗TSubclassOf?这实际上是一个确保类型安全的类模板。这实际上是对编辑器的一个提示,让您只显示从APawn.
如果您使用自定义类型并根据我到目前为止所讨论的内容,您可以找到如下内容:
TSubclassOf<class ASpeedyPawn> MySpeedyPawn;
Run Code Online (Sandbox Code Playgroud)