In C ++ 98/03 , if you have a class that is not cheap to copy (for example, int or double cheap to copy, a std::string isn 't since its copy may include allocating new heap memory, copying characters from the source to the destination, etc.), then the rule must follow the const link const std::string& :
class T { public: T(const string& s);
And then in the constructor do:
T::T(const string& s) : m_s(s) {}
However, in C ++ 11 , where movement semantics are available, the new rule looks like this: if you need a copy (and the object is cheap to move, as it usually should be), go by value and go from value:
T::T(string s)
(It would be optimal to offer a couple of overloads, passing by const & and passing by value, but probably this is not necessary in all applications, but only when you need to compress performance.)
Please note that when you do not need a copy and just need to observe this parameter, the usual C ++ 98/03 pass const & code remains valid.
source share