Why can't I dereference GetWorld pointer

Coming from C# I’m trying to learn all of the fun pointer stuff in C++ and thought I had it figured out but then this stumped me.

I saw that GetWorld() returns a pointer so in theory if I wanted to access some function on that I should be able to either do this:

GetWorld()->GetFirstPlayerController();

or this:

UWorld world = *GetWorld();
world.GetFirstPlayerController();

But the second one does not work. I get this error:

"UWorld::UWorld(const UWorld &)" (declared at line 886 of "D:\UnrealEngine\UE_4.26\Engine\Source\Runtime\Engine\Classes\Engine/World.h") is inaccessible

I thought it was just me misunderstanding how to dereference pointers but I tried it with my own class and function that returns a pointer, and both of those above techniques works fine.

So there’s something special about the UWorld class that prevents dereferencing with * but lets you dereference with → I guess? Can anyone explain what and why?

Oh ok yeah if I use the brackets around the dereference part it works fine using it inline like this (*GetWorld()).GetFirstPlayerController(); so that fixes the initial problem I ran into.

One thing I struggled to understand though was your comment about how I’m calling a constructor. I thought I wasn’t creating any new instances of UWorld there (just grabbing the existing instance and assigning it to a variable) so I didn’t think any constructor was being called. I guess that’s because I’m used to variables in C# being a reference/pointer to the object (unless its a struct). Seems like in C++ a variable actually IS the object unless its explicitly specified it is a pointer with *

Thanks for the explanation

A->B is just shorthand for (*A).B , the issue here is not the dereferencing but the copying.

Unreal generates a lot of additional code for UObjects under the hood including a private copy constructor which in simple terms means that you can’t copy UObjects from the outside (access specifiers/modifiers work mostly the same as in C#). UWorld::UWorld(const UWorld &) is that copy constructor and as the error message states it is inaccessible but you try to call it with UWorld world = *GetWorld();. UObjects therefore mostly work like normal classes in C# where you can only copy the reference but have no built-in way of copying the object itself (and for good reasons). In general, C++ within the Unreal framework is very similar to coding in C# or Java, and although I don’t wanna discourage anyone from learning it you mostly don’t need “all of the fun pointer suff” when using Unreal.