Under the assumption that the lifetime of the string is known to outlive the object that will refer to it, and that the object will not need to modify the string, then you can take a constant reference to the string in the constructor and store that in:
class StringHolder { std::string const & str; public: StringHolder( std::string const & s ) : str(s) {} };
With this solution the contents of the string will not be copied, but any change to the original string will affect the contents seen from the reference. Also, this does not allow you to change the string referred to after construction. If you need to reseat the reference you will have to do with a pointer:
class StringHolder { std::string const * str; public: StringHolder( std::string const & s ) : str(&s) {} void reset( std::string const & s ) { str = &s; } };
The fact that you need to store a pointer instead of a reference is an implementation detail, so you can (as I did) hide it from the user, or you could change the interface to take the string by pointer to make it more explicit (I prefer the former, but the later has the effect of self documentation: it is clearer that lifetimes have to be considered).
Also note that with the implementation as shown, a temporary can be used to initialize/set the string, causing undefined behavior: temporary dies, reference points to nowhere.
StringHolder(std::stringstream &stream)and havestd::string stras a class member.std::string const &, if it can be changed later, then you can pass a reference and store a pointer (beware of different lifetimes)... different requirements lead to different solutions (duh!)