简书链接:关于c参数返回返回临时变量返回局部变量地址错误的解决
文章字数:117,阅读全文大约需要1分钟
错误代码

1
2
3
4
5
FBox* UIActorExtensionMethods::GetComponentsBoundingBoxX(AActor* This)
{
FBox box1 = This->GetComponentsBoundingBox();
return &box1;//则提示返回临时变量返回局部变量地址错误
}

MDYO%J~RT3CM4H2{[T]PZ}W.png

推荐使用传递引用的办法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

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();

}