c++ - Lifetime of a thrown object caught by reference -


C ++ standard, paragraph 15.1.4 psi:

memory for temporary copy The exception to be thrown is allocated in an unspecified way, except that it is mentioned in 3.7.3.1. persists temporarily until a handler is executed for that exception.

I wonder why this code is crashing (I know that this is not the best practice):

  class magic Exceptions {Private: char * m_message; Public: Magic Exception (Const. Four * Message) {m_message = New Four [Stellen (Message) + 1]; Strcpy (m_message, message); } ~ Magic Exceptions () {cout & lt; & Lt; "Destroyer called." & Lt; & Lt; Endl; Remove [] m_message; } Char * getMessage () {return m_message; }}; Some functions with zero () {magic magic exception ("bang!"); } Int main (int argc, char * argv []) {try {someFunction ()}} grip (magic exception and ex) {cout & lt; & Lt; Ex.getMessage () & lt; & Lt; Endl; } Return 0; }  

Specifically, the Destructor of the thrown magic exposition object is called before the catch block. If I add the copy constructor to my class:

  Magic expansion (Const Magic exception and others) {cout & lt; & Lt; "Copy Constructor." & Lt; & Lt; Endl; M_message = New Four [Stellen (Other.m_ssense) + 1]; Strcpy (m_message, other.m_message); }  

Then the code works, the destroyer is called at the expected place (end of the catch block), but interestingly the copy maker is still not called. Is it optimized by the compiler (Visual C ++ 2008 optimization is off), or am I missing something?

Specifically, the distributor of the thrown magic exposition object is called before the catch block is.

Yes, as the standard is your bid, a copy is taken by the compiler, and the original (probably) discarded. Your problem is the lack of copy constructor in your original code. However, a C ++ compiler is allowed to remove (or add) Cooper constructor calls in all such circumstances.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -