To avoid keep having to use ->
and instead work directly with the object, is it acceptable practice to do:
obj x = *(new obj(...));
...
delete &obj;
This is not just poor practice, but:
obj
will store a copy of the original object created by the new
expression, and the pointer to that object returned by new
is lost;delete
a pointer to an object that was not allocated with new
. Per paragraph 5.3.5/2 of the C++11 Standard:[...] In the first alternative (delete object), the value of the operand of delete may be a null pointer value, a pointer to a non-array object created by a previous new-expression, or a pointer to a subobject (1.8) representing a base class of such an object (Clause 10). If not, the behavior is undefined.