為什么要用引用???不加也可以啊
老師在catch()中用了引用,但是我試了一下,不用引用也可以啊
#include?<iostream>using?namespace?std;void?fun1(){ throw?1.1;}int?main(){ try { fun1(); } catch(double?a) { cout<<a<<endl; } return?0;}
大神教教我……
老師在catch()中用了引用,但是我試了一下,不用引用也可以啊
#include?<iostream>using?namespace?std;void?fun1(){ throw?1.1;}int?main(){ try { fun1(); } catch(double?a) { cout<<a<<endl; } return?0;}
大神教教我……
2019-03-10
舉報(bào)
2019-03-11
不加是新在函數(shù)內(nèi)新創(chuàng)建了一個(gè)臨時(shí)變量,接收了throw傳過來的值(1.1),如果加了&,就會(huì)把1.1的引用傳過來,之后a就相當(dāng)于1.1的別名,就不用重新創(chuàng)建臨時(shí)變量,因?yàn)閍就是throw那里的1.1,可以節(jié)省一些系統(tǒng)資源。