-
Exceptions
Now suppose I have a pointer to an object and this object is deleted by another thread or earlier in the program inadvertently. Not really important why.
What kinda of exception is raised if the pointer points to a deleted object and a method is called on this object.
class CSample
{
public:
void DoSomthing()
{ . . . . . }
};
void main()
{
CSample * sample = new CSample;
. . . . .
delete sample;
. . . . .
try
{
sample->DoSomething();
}
catch(...)
{
//handle invalid reference
}
. . . .
}
while this will work. I would rather only capture the fact that this pointer is no longer valid. So what should go in in place of the ...?
-
Not sure what type of exception is thrown but I know this is considered illegal. They made the auto_ptr<>, I think that's how they spell it, for this reason.
-
OneStiffRod is right......the language doesnt promise an exception in this situation...it only says that the result is "undefined" which means it could work, could compute the answer to the meaning of life or it could crash (usually the latter). Dont do it.
Also, drop the void main...it should be int main