You're hitting the fact that you didn't mark your constructor as explicit
, and thus it can be used for implicit conversions.
new UE(true)
returns a pointer. All pointers can be implicitly converted to bool
, resulting in true
if they are non-null. UE
can be implicitly constructed from a bool
. So in effect, the pointer returned by new
is converted to bool
, which is converted to UE
using your constructor, and then the copy assignment operator of UE
is called. Of course, the memory allocated by new
is leaked.
The take-home message is: always mark your single-argument constructors as explicit
unless you actually want them to be usable for implicit conversions. By "single-argument constructor," I mean one which can be called with a single argument. Either because it has one parameter, or it has more and all parameters after the first have default arguments.