关于c++参数返回返回临时变量返回局部变量地址错误的解决
2023-07-09 本文已影响0人
吉凶以情迁
错误代码
FBox* UIActorExtensionMethods::GetComponentsBoundingBoxX(AActor* This)
{
FBox box1 = This->GetComponentsBoundingBox();
return &box1;//则提示返回临时变量返回局部变量地址错误
}
MDYO%J~RT3CM4H2{[T]PZ}W.png
推荐使用传递引用的办法。
FBox* UIActorExtensionMethods::GetComponentsBoundingBoxX(AActor* This)
{
FBox* box1 = new FBox(This->GetComponentsBoundingBox());
return box1;//如果直接 FBox =This->GetComponentsBoundingBox()然后&FBox 则提示返回临时变量返回局部变量地址错误
}
FBox UIActorExtensionMethods::GetComponentsBoundingBoxX1(AActor* This)
{
static FBox box1 = This->GetComponentsBoundingBox();
return box1;
}
void UIActorExtensionMethods::GetComponentsBoundingBoxX2(AActor* This, FBox& OutBox)
{
OutBox = This->GetComponentsBoundingBox();
}