[comp.lang.c++] Graceful Constructor Degradation

marsh@cs.rochester.edu (Brian Marsh) (06/26/89)

Is there some approved method for returning a failure code
from a constructor and reclaiming the space?  I would like
to be able to use the default allocator (well, user_new in
g++) but also to abort the constructor gracefully, allowing
the invoker to take some corrective action.

Having the constructor return 0 or any value other than that provided by 
the builtin_new() requires actually assigning to this. Since
any assignment to this precludes automatic allocation, I would
be forced to do my own allocation.

The alternative of having an out parameter which provides a status
code for the object creation works (and allows me to let dynamic
allocation proceed normally), but forces me: (a) to have the status
argument in the first place which seems unnatural, and (b) to return
a valid pointer (well quasi-valid anyway).  

In either case I would call delete before returning from the constructor.

Perhaps I am asking for too much; either I should accept doing
the call to the allocator or simply define away the return value as
not being valid.

Comments?

	Brian


-- 
ARPA: marsh@cs.rochester.edu (Brian Marsh)	USPS:	CS dept, U of Rochester
UUCP: {allegra|decvax|seismo}!rochester!marsh		Rochester, NY  14627
CSNET: marsh%cs.rochester.edu@relay.cs.net