when would I pass a pointer by reference?
thanks
When you want to be able to change the pointer value itself (in case it is not const&). Otherwise it does not amount to much, since you cannot really do anything else with that.
yes makes sense but, for instance, i have a VR Hand actor which passes a UMotionControllerComponent pointer to a GripComponent which then passes it to a GripScript UObject class. if the function on the GripScript returns true, the gripcomponent passes the motionController its owning actor.
I know you should always do a null check when working with pointers.
so in this instance, when should i do the null check:
in every function?
in the Hand actor, before it calls this chain of function calls?
or should i check in the hand actor and pass a const T*& to each function as this guarantees that the value is not null?
Hah I remember having these kinds of questions when I first started programming too.
First of all, it is not entirely true that reference always points to something. It is true you cannot create “empty” reference, but you are still able to create reference to an existing object, delete that object and voila the reference no longer points to anything valid.
That should be some hint for you, that what you proposed as “solution” to null checking is not really solution, but even more than that, if you pass T*&, you are passing reference to pointer to object, not reference to object itself, so yes, the reference would probably be valid, since the pointer variable itself will exist, but the object it points to does not have to.
The null checking is operation to check if pointer variable is not nullptr, which is useful if you can rely on the UE feature, that every TWeakObjectPtr and UPROPERTY* are set to nullptr if object they were pointing to was deleted. This is not feature of C++ standard. This way you can check for nullptr and you can be sure that if it is not nullptr, the object exists and pointer itself is valid. (I won’t go deeper into this since it is out of scope of this question)
Regarding to when to test for nullptr. It depends. I personally never check for nullptr at first, I actually do the opposite, if I am sure that object MUST exist at the time I am using it I add check(ptr != nullptr); This in Debug configuration will check if the pointer variable is valid, but is removed in shipping builds.
UE4 does not delete objects on it’s own whenever it wants. Only if the object has no more references to itself. So if you are sure the object you are using is stored somewhere preventing the GC and having it deleted is “bug” and it shouldn’t happen, then use check() macro.
If situation allows for the object to “not exists” sometimes, then you need to do classic if (ptr == nullptr)… I do this usually only in functions which are called at various times in editor where I have no control over the order of function calls (something is not yet initialized etc.)
Edit: Also if you are using dynamic_multicast delegates, it is usually good thing to check for nullptrs because they are serialized and unfortunately can be called in various times as well which makes them a bit undeterministic.