In what situation would generate an error if you don’t return something by reference for a function?
In what situation would generate an error if you don’t return something by reference
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In what situation would generate an error if you don’t return something by reference for a function?
Restricting the answer to what is being exactly asked,
Whenever you want the return value of the function to act as an l-value, and you do not return by reference then it will generate an error.
The more common example of this is overloading the
operator [](the array subscription operator), You have to return by reference in order to use the[]on the l.h.s or more correctly to use it as an l-value.An example:
Output: