There is a curious type of operator overloading with streams that takes
the form of:
friend ostream& operator<<(ostream &stream, String out);
Is it possible to define a similar type of friend operator, but rather
than using << use =?
What I would like to do is define conversion operations from high level
classes to low level ones. For example, I have my own String class
which is used everywhere. It is common that I am doing operations such
as:
someString = someInt; // so the string would get a value like
"123"
someString = somePoint; // the string might look like: "[123,
456]"
Up until now I have had to create operator specialization explicitly in
the String class for each type. This has the major drawback that the
String class must know about each type and I end up mingling core level
code with higher level code, which is no good.
In a perfect world I would like to have a class that defined its own
conversion to String implicitly. I know that I could certainly have a
method called ToString(), but I was hoping for something more
transparent. For example:
class SomeObject {
unsigned long ID;
String Name;
friend String& operator=(String &str, SomeObject &obj) {
str = obj.Name; return str; }
};
Of course this does not compile and I don't even know if such a feature
exists in C++.
Thanks