Question 16
Back to Exercise
What is wrong with int& f() { int x = 5; return x; }?
- A Nothing
- B x is destroyed when f returns, so the returned reference dangles – using it is undefined behaviour Correct
- C References cannot be returned
- D It returns a copy
Correct Answer: Option B
Return by value (int f()) instead. Returning references is fine only for objects that outlive the call, such as class members or parameters passed by reference.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!