Results 1 to 5 of 5

Thread: operator+

  1. #1

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362

    operator+

    I am making for fun a String class and I want to be able to do something like :

    myString a,b,c;
    a = "a";
    b = "b";
    c = a + b;

    But I cannot find how to use the + without changing the value of a.

    Anyone?

  2. #2
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Code:
    myString operator + (const myString& lhs, const myString& rhs)
    {
        myString ret;
        // do your stuff here...
        return ret;
    }
    That should get you started =).

    Z.

  3. #3

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    I do not need to return *this to be able to have multiple +?

    Example a = b + c + d + e;

  4. #4
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Take a look at my code.

    What happens is enssentially this:
    Code:
    in operator +:
      ret = lhs + rhs;
      return ret;
    ret = b + c;
    ret = ret + d;
    ret = ret + e;
    a = ret;
    You will only return a *this when using an <operator>= type operator, which is where you would apply the changes to the object, and return *this as a reference return value.

    Z.

  5. #5

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    Interesting
    Thx you Mister Z.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width