c++ - about the const in an operator overloading definition -
for following definition of
const vector3f operator*(const vector3f &v, float s);
there 2 const
, respective usages?
the const-reference in argument means don't change v
, can pass constant vectors (and temporaries!) function. that's thing.
the constant by-value return sort of gimmick. prevents writing things this:
vector3f v = get_vector(); vector3f w = v; (v * 1.5) = w; // outch! cannot assign constant, though, we're good.
returning by-value constant problematic, though, since interferes c++11's rvalue references , move semantics:
move_me(v * 1.5); // cannot bind `vector3f &&` :-(
because of that, , because abuse 1 showed above unlikely happen accident, it's best return value non-constant.
Comments
Post a Comment