Constructor error catching... (I guess?)

I'm having trouble finding out how to set a variable containing an object to some sort of error code during instantiation. I'm not sure whether or not this explanation is proper since I haven't been able to search for a solution.

I'm trying to accomplish something similar to this:

1
2
3
4
5
6
7
8
9
10
11
myClass object;

//Check if there was a problem during instantiation;
if(object==ERROR)
{
     print "There was a problem!";
}
else if(object==OK)
{
     print "No problems during instantiation!"
}


How can I set the value of "object" via the class constructor?
You should add and error flag member to your class. Set it if there were any problems. Then check the value of that flag.
I already know that it can be done that way, but I also know that it can be done the way I describe. If somebody knows how this is done, I would be much more comfortable to do it in the way I described.
Just provide a suitable overload for the == operator for the class



or in the case of a straight boolean test For example Good/Bad , just overload the bool operator, so you could say if (object) {...} or if (!object) {....}
Last edited on
However, I would recommend just throwing an exception. That is what most people will expect.
Topic archived. No new replies allowed.