可能重複:
Returning the address of local or temporary variable
Can a local variable's memory be accessed outside its scope?
return reference to local variable當您返回一個參考文件時它是一個未定義的行爲。到一個局部變量?
它是一個未定義的行爲,當您返回的Ref。到一個局部變量?
int & func(){
int x = 10;
return x;
}
int main() {
int &y = func();
cout << y << endl;
}
我不確定在本規範中「局部變量」是否有特殊意義,但我想這對於沒有靜態存儲持續時間的任何局部變量都是未定義的行爲。 –