#include<iostream>
#include<conio.h>
using namespace std;
/* test class: created the reference of
abc class locally in function getRef()
and returned the reference.*/
class abc {
int var;
public:
abc():var(5) {}
abc(int v):var(v) {}
abc & getRef() {
abc myref(9);
return myref;
}
void disp() {
cout<<var<<endl;
}
};
int main() {
abc a;
abc b=a.getRef();
b.disp(); /* this statement executed perfectly.
I think compiler should throw error here. */
getch();
return 0;
}
컴파일러에서 컴파일 오류를 발생시켜야합니다. 설명 해주십시오 ?함수에서 로컬 변수를 참조하는 중 오류가 발생하지 않습니다.
중복 가능성 http://stackoverflow.com/questions/6441218/can-a- local-variables-accessible-outside-its-scope) –