#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;
}
编译器应该抛出编译错误。请解释 ?