From: rgp@mpd.tandem.com (Ramon Pantin)
Newsgroups: comp.lang.c++
Subject: Re: Return value for the constructors.
Date: 21 Aug 92 02:28:26 GMT
Distribution: usa
Organization: Tandem Computers, Micro-Products Division


In article <matt.714318343@centerline.com> matt@centerline.com (Matt Landau) writes:
>In <2273@devnull.mpd.tandem.com> rgp@mpd.tandem.com (Ramon Pantin) writes:
>>>>Sure there is: Overload operator new()...
>>>
>>>*****>	Clever...but it's not reentrant.
>
>>Paul, that is a moot point given that the object might not always be
>>created thru new.  All the flag setting/checking by Josh's overloaded
>>new ends up being ignored for automatic objects (i.e. local variables).
>>The fact that an object wants to allocate memory thru new for its internal
>>use doesn't imply that the object itself will always be created thru new.
>
>Of course it's fairly straightforward to FORCE all objects of a given
>class to be created via the class operator new (some some other delegated
>creation function).  Simply design that class so that it has no public
>constructors, and make the default constructor private.  The compiler
>will now refuse to let either application-level code or subclasses call
>any constructor for the class.  If you want subclasses to have direct
>access to the constructors, make them protected instead of private.

Lets see if I follow the logic of this thread (very liberally paraphrased):

- Initial question:
	How can I tell if there was an error during the execution
	of a constructor?

- Initial reply:
	You can't return a value in a constructor.  If you could do so,
	then common idioms where a temporary object is created:
		z = w * complex(real, imag);
	could not be used.
	You will have to wait until exception handling is available so
	that the construction can throw an exception on error cases.

- Another reply (or what it boiled to):
	You can tell if a memory allocation failed when you invoke new
	(in that case the constructor is not called at all).

- From which followed:
	Overload operator new and have some static variable thru which
	the constructors and the overloaded new communicate so that it
	can be checked by the overloaded new and a NULL returned by it
	instead.
  I'm not even sure that this can be done.  I have never overloaded new
  but I thought that the overloaded new was used only to allocate the
  memory (does it have to invoke the constructor by "hand"?), I'm not
  sure at what point after the construction would the overloaded operator
  new get control so that it can do the flag checking stuff.  Can somebody
  follow up on this?

- Problem:
	Automatic objects and arrays don't get their storage allocated
	thru new.

- Latest (this) post:
	Forbid the construction of automatic objects so that they have
	to be always allocated thru new.


So, to get an error code out of a constructor we should (note that I
DON'T agree with all this!):

- Forbid the creation of automatic variables and arrays, i.e. make
  the constructors non-public.

- Do the new/constructor thing.

- Have some public static member function that creates objects.

Sounds pretty convuluted, don't you think?
No wonder nobody seems to write simple code anymore!

Ramon Pantin
Newsgroups: comp.lang.c++