Mixing passing by pointers and passing by reference in TakeDamage()

Hello,

Looking at the following:

float AShooterCharacter::TakeDamage(float DamageAmount, struct FDamageEvent const& DamageEvent, class AController* EventInstigator, AActor* DamageCauser)
{
}

Some of the parameters in AShooterCharacter::TakeDamage() are passed by pointer and some by reference (and some by value). I can more-or-less understand the difference, but according to the first link below,

“If there’s ever a chance that you could want to pass “no object”, then use a pointer instead of a reference. Also, passing by pointer allows you to explicitly see at the call site whether the object is passed by value or by reference:”

These both seem like very good reasons to use pointers. Is there a reason why DamageEvent is passed in by reference?

(I’ve also included a second link I looked at when trying to work out the difference between passing by reference and pointer)

Because there’s always a damage event. In most cases only UObject’s are passed via pointer as they are dynamically allocated.

Thanks, do you have any good resources that explain about dynamic allocation?

https://www.learncpp.com/cpp-tutorial/dynamic-memory-allocation-with-new-and-delete/

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms