I don't understand how this code works:
class AAA {
public:
short a, b;
};
AAA &getRef() {
AAA aaa = {2, 6};
return aaa;
} // 'aaa' is destroyed here right?
int main() {
AAA &ref = getRef();
cout << ref.a << ", " << ref.b << endl;
cin.ignore();
return 0;
}
Shouldn't there be an error trying to access ref.a
and ref.b
? When I use pointers, I don't get an error either. I mean,this prints "2, 6" every single time.
EDIT: Is it because the memory is still set to those numbers?