问题描述:
补充阅读
<code class="hljs cpp has-numbering"><span style="font-family:KaiTi_GB2312;font-size:18px;color:#ff6666;"><strong><span class="hljs-preprocessor">#include <iostream></span> <span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>; <span class="hljs-keyword">void</span> f(); <span class="hljs-keyword">class</span> T { <span class="hljs-keyword">public</span>: T( ) { <span class="hljs-built_in">cout</span><<<span class="hljs-string">"constructor"</span><<endl; <span class="hljs-keyword">try</span> { <span class="hljs-keyword">throw</span> <span class="hljs-string">"exception"</span>; } <span class="hljs-keyword">catch</span>(<span class="hljs-keyword">char</span>*) { <span class="hljs-built_in">cout</span><<<span class="hljs-string">"exception"</span><<endl; } <span class="hljs-keyword">throw</span> <span class="hljs-string">"exception"</span>; } ~T( ) { <span class="hljs-built_in">cout</span><<<span class="hljs-string">"destructor"</span>; } }; <span class="hljs-keyword">int</span> main() { <span class="hljs-built_in">cout</span><<<span class="hljs-string">"main function"</span><< endl; <span class="hljs-keyword">try</span> { f( ); } <span class="hljs-keyword">catch</span>(<span class="hljs-keyword">char</span> *) { <span class="hljs-built_in">cout</span><<<span class="hljs-string">"exception2"</span><<endl; } <span class="hljs-built_in">cout</span><<<span class="hljs-string">"main function"</span><<endl; <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; } <span class="hljs-keyword">void</span> f( ) { T t; }</strong></span></code>
预计运行结果:
main function
constructor
exception
exception
destructor
exception2
main function
实际运行结果:
运行时候指向一个空字符指针!应该
char*p="exception";throw p;
<code class="hljs cpp has-numbering"></code><p></p><span style="font-family: KaiTi_GB2312; font-size: 18px; "><strong><span class="hljs-string">2"</span></strong></span>
时间: 2024-10-29 03:25:19