friend operator= ?

Discussion in 'C++' started by steve@walkereffects.com, Dec 7, 2005.

  1. Guest

    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
    , Dec 7, 2005
    #1
    1. Advertising

  2. On 7 Dec 2005 11:17:28 -0800, 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
    Bob Hairgrove, Dec 7, 2005
    #2
    1. Advertising

  3. Guoping Guest

    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.
    Guoping, Dec 7, 2005
    #3
  4. Guest

    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.
    , Dec 8, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Avinash

    Friend and Operator []

    Avinash, Jun 24, 2003, in forum: C++
    Replies:
    5
    Views:
    1,113
    John Carson
    Jun 24, 2003
  2. Nitin Bhardwaj
    Replies:
    8
    Views:
    836
    Andrey Tarasevich
    Jul 14, 2003
  3. Alexander Grigoriev
    Replies:
    6
    Views:
    2,538
    Kevin Goodsell
    Sep 12, 2003
  4. John Cho
    Replies:
    1
    Views:
    292
    Jonathan Turkanis
    Mar 3, 2004
  5. Peter
    Replies:
    2
    Views:
    262
    Öö Tiib
    Jun 6, 2013
Loading...

Share This Page