TSharedPtr
also implements a cast operator to bool
which does the same thing as IsValid
.
This means that it is not necessary to call IsValid
to check the pointer, you can just compare it as if it was a raw pointer. The following two lines are identical:
if (SessionInterface.IsValid()) ...
if (SessionInterface) ...
The rest is a matter of preference, like when checking for nullptr
. Some people prefer if (Ptr != nullptr)
, while others prefer if (Ptr)
(and their counterparts if (Ptr == nullptr)
vs if (!Ptr)
).
–
Of related interest, the C++ core guidelines, which tries to standardized the way to use and write C++ to avoid common mistakes, recommends the shorter version:
if (Ptr)
if (!Ptr)