I consider writing a class that behaves differently, depending on its construction method:
Example:
// #1 - Own an object
class MyClass {
MyClass(std::string const& str) : m_str(str) {}
std::string m_str;
}
// #2 - Share an object (Can be done by pointer as well)
class MyClass {
MyClass(std::string& str) : m_str(str) {}
std::string& m_str;
}
- Is this a good idea to have both behaviors in the same class?
- What is the best way to implement such a class?
Try this:
Now just make sure you always use
MyClass::strfor everything.