UE_LOG and TCHAR

Looking at this code :
UE_LOG(LogTemp, Warning, TEXT(“PlayerView is Location: %s , Position: %s”),
*PlayerViewPointLoc.ToString(),
*PLayerViewPointRotation.ToString()
)

It says UE_LOG is expecting a TCHAR right? so you cant just use .ToString(), you have to add a * to the front, which makes it a pointer right?

Well if TCHAR is just a Char, and a method wants a Char, how does adding a pointer to a String parse it into all the single chars? I would assume giving it a pointer would return the occurrence of the first char or multiple pointers to every char in the string.
If any case it makes me feel like you would need some kind of for loop here to parse through every char, we dont? whats happening behind the scenes?

1 Like

Look at the api info onFString.

And TEXT wants a *TCHAR… old school C -style string I would assume, a pointer to a (series) of TCHAR, terminated by a null if it is indeed c-type string.

So the API is saying an FString is an array of TCHAR.

TEXT wants a Pointer (*) to TCHAR

So putting a pointer (*) infront of FString make it a a Pointer to an Array of TChars?

It still feels like… theres something going on behind the scenes im not understanding. like its ability to loop through an Array of TChars when it only wants one TCHAR?

One of the more powerful features of C++ is operator overloading which lets operators like +,==,* or whatever behave differently in different contexts.

So in this case, the * which means “create a pointer” in a type declaration or “dereference pointer” in an assignment means something (slightly) different when in front of an FString in a context where the return type is *TCHAR. There the * means “return a *TCHAR version of the string.” It is no coincidence that * was chosen since it behaves like a dereference, its just a little smarter about it by providing the type needed.

BTW - The reference to this operator and a bunch more shows up lower down on the FString documentation page as operator*() and if you check out some of the other overloaded operators like /= you will see there are different versions of the same operator based on the expected return type! Pretty cool…

1 Like

As far as “looping” through the TCHAR and C-style strings: Originally in C a string was a null (/00) terminated array of chars, Each char was an 8 bit value that represented a alphanumeric character in ASCII code.

Pointers and arrays are intimately related in C and C++ where stepping through the array is equivalent to incrementing a pointer whose address is the start of the array. So char* theChars == theChars[0], and (theChars++) == theChars[1] (it is one of the ways pointers are very powerful in C, C++. look up pointer arithmetic for more info)

So you can pass a c-style string using a pointer to the first character of that string and the string terminates when you reach a char == 00, thus it is called null-terminated string.

In C++ I think the TCHAR and other String types are fully functional objects that are more complex than the simple c-type null terminated string but are designed to behave the same way (with additional functionality) When it comes down to it the low level implementation is not what matters, as long as you can get and pass a pointer to the desired type, the compiler knows how to handle the string.

1 Like

Ah this makes alot more sense,
overloaded methods and the behind the scenes workings of TCHAR.

I have only been taught Java so this is a pretty new concept to me.

Thank you for your help

Privacy & Terms