The short answer is this wouldn't compile. You're attempting to assign a Foo *
to Foo
. eg. on gcc this generates the compile error:
In function 'void myFunction()':
error: no match for 'operator=' (operand types are 'Foo' and 'Foo*')
But let's just pretend for arguments sake this isn't an issue. What would happen above is foo
gets default constructed on the local stack of myFunction
. On exit, foo
goes out of scope and its destructor is executed so no leak here.
Now new Foo(params);
, on the other hand, is a different story. This allocates space on the free store for a Foo
instance and calls the Foo(params)
constructor. When myFunction
exits, the pointer to this dynamically allocated Foo
will still exist(even if you can't refer to it) since there's no delete
operator paired with it. Unless you delete
this pointer elsewhere in your code this will leak.