Oh, I have. But I kind of doubt this is a compiler bug...
Announcement
Collapse
No announcement yet.
valgrind sucks
Collapse
X
-
Originally posted by Kuciwalker View PostI don't believe you can provide a universally applicable answer. It depends on the application - in particular, it depends on the reason for needing the destructor in the first place.
So, for a constructor you should throw an exception. Can't use return values, they have no return value. You could also just log it, but this isn't really helpful.
For a destructor, you cannot do a return value...obviously. You also should not under any circumstances throw an exception. The only thing you could possibly do is log it, or ignore it. Logging is preferred. The standard allows you to throw an exception, but it is very dangerous.
That's the easy answer, but in the interview the meat is the follow-up question: Why not throw an exception?.
The main problem is that it puts the C++ runtime in a bad state if an exception is thrown during the process of unwinding the stack. The stack gets unwound when code throws an exception (so "throw Blah()" will unwind the stack all the way from the throw statement to the catch statement). The process of unwinding destroys all local objects that got popped. If one of those objects throws another exception, the C++ runtime has no proper recourse. Say you do a throw Blah() and it starts unwinding objects, one of which has a destructor which throws a Blee(). At this point, it doesn't know which catch to go to. Either choice will ignore at least one catch block.
So the C++ standard defines a course of action. It calls terminate(). The app dies, period. If you didn't deallocate everything, tough ****, it's done.
If that makes sense.
It's a terrific question because it not only asks about good programming practice, but to answer the "why" question the person needs to have a pretty clear understanding of how C++ works or at least have very strong ability to reason through things, if given a couple hints."The issue is there are still many people out there that use religion as a crutch for bigotry and hate. Like Ben."
Ben Kenobi: "That means I'm doing something right. "
Comment
-
Another question I'll always ask regarding destructors is: "When should your destructor be virtual, and why?"
For similar reasons to the other question."The issue is there are still many people out there that use religion as a crutch for bigotry and hate. Like Ben."
Ben Kenobi: "That means I'm doing something right. "
Comment
-
Originally posted by Asher View PostAssuming you'd like to know of the failures, there's only one way.
So, for a constructor you should throw an exception. Can't use return values, they have no return value. You could also just log it, but this isn't really helpful.
For a destructor, you cannot do a return value...obviously. You also should not under any circumstances throw an exception. The only thing you could possibly do is log it, or ignore it. Logging is preferred. The standard allows you to throw an exception, but it is very dangerous.
That's the easy answer, but in the interview the meat is the follow-up question: Why not throw an exception?.
The main problem is that it puts the C++ runtime in a bad state if an exception is thrown during the process of unwinding the stack. The stack gets unwound when code throws an exception (so "throw Blah()" will unwind the stack all the way from the throw statement to the catch statement). The process of unwinding destroys all local objects that got popped. If one of those objects throws another exception, the C++ runtime has no proper recourse. Say you do a throw Blah() and it starts unwinding objects, one of which has a destructor which throws a Blee(). At this point, it doesn't know which catch to go to. Either choice will ignore at least one catch block.
So the C++ standard defines a course of action. It calls terminate(). The app dies, period. If you didn't deallocate everything, tough ****, it's done.
If that makes sense.
It's a terrific question because it not only asks about good programming practice, but to answer the "why" question the person needs to have a pretty clear understanding of how C++ works or at least have very strong ability to reason through things, if given a couple hints.
Comment
Comment