UE4 C++ Super类型

编程入门 行业动态 更新时间:2024-10-10 12:25:12

UE4 C++ Super<a href=https://www.elefans.com/category/jswz/34/1771355.html style=类型"/>

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::AMyMyActorGEngine->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));
}

结果是:

更多推荐

UE4 C++ Super类型

本文发布于:2024-02-06 04:20:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1746211.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   Super

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!