jordan@Morgan.COM (Jordan Hayes) (06/30/89)
Is there any convention for how to return from a constructor that has
"failed" ... for instance, consider a fascist parent who won't let you
look at TV during school hours ... the constructor could check the time
of day, and if it's not during school hours, allocate you a TV object,
and your code continues. What to do if it's 1pm? Can a constructor
"return NULL" so you could say
TV *my_tv;
if ((my_tv = new TV("HBO")) == NULL)
go_read_a_book();
I took a quick look through a bunch of C++ code I have lying around,
but couldn't find any exmples of constructors that "return" something.
Further, if a constructor decided to "abort" a construction, could the
destructor get called?
Or, am I doomed to have to write
my_tv = new TV("ESPN");
if (my_tv->Valid() == FALSE) {
delete my_tv;
go_read_a_book();
}
Thanks.
/jordan