Hello all
Just for your sanity let me clarify something:
In the code to display the object’s name in the console, we use the UE_LOG
macro which demands a pointer as the last parameter.
void UPositionReport::BeginPlay() {
Super::BeginPlay();
FString ObjectName = GetOwner()->GetName();
UE_LOG(LogTemp, Warning, TEXT("Position report reporting for duty on %s!"), *ObjectName)
}
Ben tells us to use the *ObjectName
construction to get the module to compile.
He mentions we are dereferencing the FString ObjectName
.
But that is not really true.
Actually FString
is an actual class, and you cannot dereference an instance as ObjectName
:
FString ObjectName ...; // ObjectName is an instance of FString, you CANNOT dereference with *ObjectName
FString *ObjectName ...; // ObjectName is a pointer to FString, you CAN dereference with *ObjectName
So. Why the code compiles? Why it Works?
Actually what we are doing is not pointer dereferncing nor pointer-related stuff.
The trick here is that there is a custom operator defined with the name *
. This *
operator is defined in the file UnrealString.h
as follows:
/**
* Get pointer to the string
*
* @Return Pointer to Array of TCHAR if Num, otherwise the empty string
*/
FORCEINLINE const TCHAR* operator*() const
{
return Data.Num() ? Data.GetData() : TEXT("");
}
So by using the *ObjectName
construction we are really calling this code above, and not doing any C++ pointer dereferencig.
It is a bit difficult for us beginners to find out what is C++ stuff and what is custom code (as the operator*()
above), but once you learn the relatively simple rules of C++, you start to detect this kind of constructions.
Ramón Gil Moreno