#include <iostream>
using namespace std;
int main (int argc, char* const argv[]) {
int a = 20;
cout<<"address of a is "<<&a<<endl;
try{
throw a;
}
catch (int& z) {
cout<<"address of z is "<<&z<<endl;
}
return 0;
}
a
的地址與z
的地址不同。這意味着引用在try catch中不起作用。如果沒有,那爲什麼編譯器不會產生任何錯誤?上面的代碼是什麼意思?參考不按預期在嘗試捕獲中工作
**當您_throw_時複製** **。 – 2012-03-22 12:46:18
@Adriano這意味着引用失去了它們在上述情況下使用的含義嗎? – niting112 2012-03-22 12:48:00
我想這只是一個例子(所以我不會問你爲什麼抓到一個int的引用)。引用不會失去它們的含義,但是當您使用_value類型_(因爲我們的.NET朋友會調用它們),您會傳遞_throw_原始值的副本。 – 2012-03-22 12:55:27