Write the setter to take a const&
:
void setProperty(Property const& p) {m_property = p;}
and in C++11, write an overload that takes &&
as well:
void setProperty(Property&& p) {m_property = std::move(p);}
It's nice to use the "shortcut" method of writing a single overload that takes the property by value:
void setProperty(Property p) {m_property = std::move(p);}
but be aware that when passed an lvalue, this requires memory to be allocated simultaneously for 3 objects - the original, the member, and the parameter - for a short time.
If you're feeling especially jaunty, you can amaze your friends and family by writing a perfect forwarding setter:
template <typename T>
void setProperty(T&& t) {m_property = std::forward<T>(t);}
which subsumes both the const&
and &&
case, as well as being capable of accepting any type that is assignable to m_property
. Be aware that this is hell on readability, and results in "interesting" error messages when misused.