For the following definition of
const vector3F operator*(const vector3F &v, float s);
There are two const, what are their respective usages?
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.
The const-reference in the argument means that you don’t change
v, so you can pass constant vectors (and temporaries!) to the function. That’s a Good Thing.The constant by-value return is sort of a gimmick. It prevents you from writing things like this:
Returning by-value as constant is problematic, though, since it interferes with C++11’s rvalue references and move semantics:
Because of that, and because an abuse like the one I showed above is fairly unlikely to happen by accident, it’s probably best to return by value only as non-constant.