Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   friend operator= ? (http://www.velocityreviews.com/forums/t450357-friend-operator.html)

steve@walkereffects.com 12-07-2005 07:17 PM

friend operator= ?
 
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


Bob Hairgrove 12-07-2005 07:44 PM

Re: friend operator= ?
 
On 7 Dec 2005 11:17:28 -0800, steve@walkereffects.com wrote:

>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.

--
Bob Hairgrove
NoSpamPlease@Home.com

Guoping 12-07-2005 07:50 PM

Re: friend operator= ?
 
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.


steve@walkereffects.com 12-08-2005 02:29 AM

Re: friend operator= ?
 
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.



All times are GMT. The time now is 04:07 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.