Learning some c++ through tutorials, why does this GetOwner require to be a pointer, but the other not

Hi, just doing some basic c++ tutorials and I was wonder why the first GetOwner in the log line does not need to be a pointer, but in the second log line it does.

It’s not about GetOwner(), but about the value you format.

In the first case, you are using “%f” format which expects a float. The term GetOwner()->GetActorRotation().Yaw evaluates as a float which is fine.

In second case, “%s” format expects a “string” which in C++ terms generally means a char array, or char*. Or in this specific case, since we are working with wide characters (which is what the TEXT macro specifies) it expects a wchar_t array, or wchar_t*, or TCHAR* (same thing).

The term GetOwner()->GetActorLabel() is not of type TCHAR* though, it is an FString. FString is an UnrealEngine-specific wrapper arround TCHAR* that provides loads of convenience functionality. However for some reason (probably a good one but I don’t know precisely why), there is no automatic conversion from FString to TCHAR*. If you try use GetOwner()->GetActorLabel() for the “%s” format you’ll get an error message from the compiler roughly saying “Hey I cannot use this value of type FString for the %s format, please give me something that fits”.

The conversion has to be done manually, and the * operator is one way to do it.
If you look into FString code you can see they overloaded the * operator to return the TCHAR* data wrapped within the FString :
image

As you can see this doesn’t really have anything to do with pointer de-referencing though. Due to operators overloading you can completely change C++ semantics from what they are supposed to be.

1 Like

Thank you for the detailed explanation!