UE4 C++ Super类型

在UE4 C++工程中,当子类重写父类虚函数时,常常会见到Super类型,如下:

// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
	Super::BeginPlay();
}

// Called every frame
void AMyActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

Super是父类的类型别名,其定义是:Typedef 父类名 子类名::Super。在子类中使用Super,是对父类成员函数、成员变量的调用。

下面举例说明:
分别创建父类AMyActor、子类AMyMyActor,在AMyActor中设置虚函数Content()和变量value:

//声明
public:
	void virtual Content();
	int value;
//定义
AMyActor::AMyActor()
{
	PrimaryActorTick.bCanEverTick = true;
	value = 1;
}

void AMyActor::Content()
{
	GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("This is MyActor"));
}

在子类中重写Content()函数:

//声明
public:
	void virtual Content() override;
//定义
void AMyMyActor::Content()
{
	Super::Content(); //Typedef AMyActor Super::AMyMyActor
	GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("This is MyMyActor"));
	Super::value = 2;
	GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, FString::Printf(TEXT("value = %d"), value));
}

结果是:
在这里插入图片描述

发布了5 篇原创文章 · 获赞 10 · 访问量 439

猜你喜欢

转载自blog.csdn.net/weixin_43575837/article/details/103270755
今日推荐