Download

What is the purpose of Dereferencing non pointer variable

Trying to get my head around strings in unreal, and came across this code

std::string someString = "Hello!";
FString layerName(someString .c_str());
UE_LOG(LogTemp, Warning, TEXT("%s"), *layerName);

layerName is not a pointer, its an FString object. So my question is, what do you get when you dereference non pointer object.

I have never seen this befor, except in CSharp where alll objects are treated as pointers. In C++ class objects are not pointers.

So please educate me on this use case of dereference operator thats new to me.
Cause its dereference non pointer variable

cheers

In C++ there’s something called operator overloading. You can overload most operators to make them do what you want and then they are just functions when you call them. The class FString overloads the indirection operator * to return a pointer to the underlying char array, it does not actually have anything to do with what the operator does when you use it on a pointer (which is a built-in functionality and not a function). You could acutally just write it like this as well

UE_LOG(LogTemp, Warning, TEXT("%s"), layerName.operator*());

which highlights the fact that it is just a function. It is very common to overload operators, e.g. you probably already know that you can add two FVectors with +. That is an overloaded operator as well because otherwise how would the compiler know what adding two custom types like FVector is even supposed to mean? It only knows how to use the + for primitive types by default (int, float, etc.).

Which contains a pointer to the array of chars in memory.
So you give to the log function the address of the first char and the language knows it’s going to read a string up to the next null terminator in that array of chars: ‘\0’

Thanks again , that settles my doubt, i was starting to get very confused cause i done c++ 20 years but relatively new to unreal.

I searched the source code for FString briefly before asking the question but the file is so big i missed the * Operator. So i got to thinking its another special Epic magic i dont know about.

Probably not the the best use of operator * , cause its confusing and alarming use of the operator * since operand is not a pointer.

They should have just use a function call rather operator*, I still dont knkow which the equivalent FString function is for the operator , I already tried the Get Array from fstring .

Thanks for clearing that up.

I’m surprised to hear you say that with so much C++ experience. It’s pretty common for types that aren’t pointers but are basically equivalent to pointers. This goes for both types with pointer in the name (smart/weak pointers) and those without (every iterator I’ve ever seen overloads it regardless of whether or not the internals are actually a pointer).

There isn’t a direct single function equivalent, but String.GetCharArray( ).GetData( ) is basically what it’s doing (it omits the GetCharArray call since it can just use the member variable directly that function normally returns).

There isn’t a direct single function equivalent, but String.GetCharArray( ).GetData( ) is basically what it’s doing (it omits the GetCharArray call since it can just use the member variable directly that function normally returns).

Thanks for the detailed explanation MagForceSeven

I can understand smart pointers using this syntax cause i use it with smart pointers, but they are pointers., at least in name.

And i can now see how Strings and iterators can have same design, but that use case is new to me for non pointer types.

Now that i know its common ill just stick to the *operator too and join the club.
cheers.

1 Like