Question on C++ operator overload within Unreal Engine

Hi, I’m just a starter in both C++ and Unreal Engine5, I have run into confusion about the following lines when I was learning, which is:

there is an obj “TSubclassOf ScoreDisplay_Widget_Class;” which I’ve framed red.

we can see it’s considered valid in the “if” statement where Im putting obj ScoreDisplay_Widget_Class with the operator “!=” and keyword “nullptr”, which I believe is checking if it is a null pointer.

"if (ScoreDisplay_Widget_Class != nullptr) " be noticed the operator “!=” has no overloaded version for the class “TSubclassOf”

but this thought come to me:
how come an operator that has never been overloaded before validly accepts a specific class operand ???

whereas here I wrote a few lines based on the basically same idea as above, but the compiler says it needs an overloaded version of operator “!=” for operand “A” and “nullptr”. why???

hi, i’m still new to c++ too

in the first example, i think you need to include the header for UUserWidget

in the second example, you are comparing A and nullptr, but A is not a pointer, it is an object

also, i just noticed, ScoreDisplay_Widget_Class is not a pointer, so you cannot compare to nullptr

TSubclassOf has an implicit conversion operator so it doesn’t need to overload operator!=. See Compiler Explorer for the same principle applied to your example. You can read more about user-defined conversion functions and the conversions applied in the background

1 Like

thx, I think u are right on this