friend operator= ?

S

steve

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
 
B

Bob Hairgrove

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]"

Isn't this doing low level to higher level conversion? Or does
"someInt" have some other type than int?
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.

Why re-invent the wheel? std::stringstream does all this perfectly
well.
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++.

It looks like you have attempted to define a member operator=() for
SomeObject in the above example. If you want to assign to String, you
need an assignment operator in String, not SomeObject. Besides, the
arguments are wrong for a member assignment operator.

There are big problems with unwanted implicit conversions which arise
when you define operators such as that. Better to use explicit
functions such as "ToString".

I would suggest using the STL stringstream class to implement all of
your conversions because that way, you don't need to re-invent the
wheel. Overload operator<< for ostream the usual way for your custom
classes, then use the str() member function to retrieve the string.
That has the advantage that you can also use the same stream functions
to read and write your data to a file, for example.
 
G

Guoping

Use type conversion operator in your classes to be converted to
YourString class:

class SomeObject
{
operator YourString() const { /* build YourString from SomeObject
here */ }
};

Note that you need to provide a copy ctor for YourString. The drawback
is that this is less efficient than a YourString(const SomeObject&)
ctor.
 
S

steve

Thanks that did the trick! I remember now seeing the conversion
operator before but had forgotten about it. So now I can do assignments
such as:

myString = someObject;

And get the behavior I want without having to explicitly say
someObject.ToString() every time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top