Can I overload existing function/operator in existing class?
I was trying to do:
#include
#include
using namespace std;
I defer to Benjamin's answer for creating a stream-like interface on a string
object. However, you could use a stringstream
instead.
#include
std::istringstream ss;
ss << anything_you_want;
std::string s = ss.str(); // get the resulting string
ss.str(std::string()); // clear the string buffer in the stringstream.
This gives you the stream-like interface you want on a string
without needing to define a new function.
This technique can be used generally to extend the functionality of a string
. That is, defining a wrapper class that provides the extended functionality, and the wrapper class also provides access to the underlying string
.