For a local variable it doesn’t make sense as you note. It’s when passing to an from functions.
Consider the following code:
void Print(FString Msg);
void Foo()
{
FString Name = TEXT("Dan");
Print(Name);
}
Here Print
takes the parameter “by value” which means to copy. FString stores a dynamically sized string, which when you copy one you’ll have to dynamically allocate enough memory to hold the new string then copy each character over. Then when that object goes out of scope that memory will be freed.
Assuming Print
does exactly what it says, did it need a copy? Did it really need to do that work when all it will do is read the string? That’s where references comes in, now you aren’t dynamically allocating memory and copying all of the characters over, and in this example it should also be const &
as it would be rather surprising if a print function modified what I passed to it.