Question 3
Back to Exercise
Why is void print(const std::string& s) preferred to void print(std::string s)?
- A It allows modifying s
- B It avoids copying the string while preventing modification, and still accepts temporaries Correct
- C It is required for strings
- D It makes the function inline
Correct Answer: Option B
Copying a long string allocates memory. A const reference reads the caller's string directly and safely.
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!