C++ - Why are pointers used so much?

Using references is inconvenient since they cannot be NULL, so you would have to use a wrapper class if the object being referenced is allowed to not exist. Passing objects by value results in excessive memory copying (move semantics help with that, but they are tricky) and is not always possible (you may want to modify original object and not its copy).

This is a generic answer, there are particular cases where some pointers could be avoided for references. Although be careful when applying advices from the web: a lot of them are written with security/robustness in mind, and aren’t helpful when you want performance.

Okay, this may seem like a strange question, but why are pointers used so much in unreal engine? I am new to programming in C++, and come from Java/C#. I have read repeatedly that you should avoid pointers and use references and values instead, so why are they used so much in the engine. There must be a reason for this. For example:

AActor* UWorld::SpawnActor
(
    UClass*         Class,
    FName           InName,
    FVector const*  Location,
    FRotator const* Rotation,
    AActor*         Template,
    bool            bNoCollisionFail,
    bool            bRemoteOwned,
    AActor*         Owner,
    APawn*          Instigator,
    bool            bNoFail,
    ULevel*         OverrideLevel,
    bool            bDeferConstruction
)

Why does this return a pointer (and not a value) and take in so many pointer arguments and not reference arguments.

EDIT - So basically references (const) are used when by value would be less efficient to copy, but cannot be null; and pointers are used when passing by value is inefficient and null is an option. Also should I use nullptr or NULL in the engine? Also, why is the actor returned as pointer, not value?

Thank you

In Java, all objects and arrays are reference types, which means that they can be passed around efficiently without explicitly having to declare pointer types. Under the hood, however, they, too, are being passed as pointers - you just don’t see it in the syntax.

In C++, objects and arrays are passed around as values. Using pointers is the idiomatic way of specifying that you want to refer to an object or array by reference, not by value. The C++ syntax requires you to be more specific as to whether you handle such objects by reference or by value, while in Java it is assumed to be by reference by default.

To be more specific, there are actually two reference type concepts in C++: pointers and references. Pointers are declared with a star (*) and references with an ampersand (&). A C++ pointer can be null, just like a Java object references can be null. A C++ reference on the other hand must always be assigned and can generally be assumed to be not null.

while in Java it is assumed to be by
reference by default

FWIW: One of the main performance problems with Java is that it doesn’t have user-definable value types at all (default or no.) This is one of the main improvements of C# over Java.

So let me get this straight, for the arguments pointers are used because it would allow them to be null, and the return value is pointer because the original should be used and not a copy?

So basically references are used when by value would be less efficient to copy, but cannot be null; and pointers are used when passing by value is inefficient and null is an option. Also should I use nullptr or NULL in the engine?

They are both correct. However, using nullptr is better because it cannot be assigned to a non-pointer type. For instance:

int32 Foo = NULL; <--- Compiles fine but is not "correct"
int32 Foo1 = nullptr; <--- Compile error