Suppose I have a class ThisHasAStringMember
. Assume it has a private member which is a string, and I want to efficiently get the string value, preferring a move over a copy where possible. Would the following two constructors accomplish that?
class ThisHasAStringMember
{
public:
// ctors
ThisHasAStringMember(const std::string str) : m_str(str) {}
ThisHasAStringMember(std::string &&str) : m_str(std::move(str)) {}
// getter (no setter)
std::string value() { return m_str; }
private:
std::string m_str;
}
Do I need the double ampersand before the str
parameter in the second constructor?
Is this the right way to accomplish this?
Aucun commentaire:
Enregistrer un commentaire