overloading the + operator

This is a discussion on overloading the + operator within the C++ Programming forums, part of the General Programming Boards category; hi im learning about overloading operators in my class and i dont really understand it. anyways i got an assignment ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    20

    overloading the + operator

    hi im learning about overloading operators in my class and i dont really understand it. anyways i got an assignment to overload the + operator so i can add two strings togther into a third string and i was hoping someone could help me with it.
    IE:
    string1=happy
    string2=birthday
    string3=string1+string2

    string3 would print happy birthday

    thanks in advance any help is appreicated

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I assume you mean that you are overloading the operator+ for your own string class, since the C++ string class already has operator+ overloaded.

    Do you know how to write the prototype for the function? That would be a good place to start. See if you can get it to work just returning the first string instead of adding the second to it. Then move on to combining the strings. Post code and ask questions if you get stuck.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    20
    its like
    friend operator+ ();?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You have to add a return value. You also have to add the parameters to the function. All three of those depend on the name of your string class.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Also, you don't need the friend keyword to overload operators. You've probably seen someone overload the << operator(refering to an already overloaded version of it), in which case, you're making it friend so you can access private ostream members. Also, understand that overloading operators are very restricted. You can't change the number of arguements, and atleast one of the operands must be user defined, etc etc...
    Code:
    myClass operator+(const myClass &oprnd) const;
    Last edited by SlyMaelstrom; 04-17-2006 at 06:56 PM.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    That prototype isn't correct, you shouldn't be returning a reference to a temporary object, and operator+ shouldn't be modifying either of its operands.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    As I was taught, you return a reference as some compilers have trouble chaining the operator without returning it as a reference. While I'm sure that isn't the case in modern compilers, it's simply how I write it because it's how I needed to write it to get a proper grade. It may yield a warning, though, on a modern compiler. Off of a guess, he just said that because of how the insertion and extraction operators work. He wasn't very good at what he did.

    And yes, you're right, for const correctness, I should have made the operator function constant.
    Last edited by SlyMaelstrom; 04-17-2006 at 06:41 PM.
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I don't see how making it a reference would make chaining easier. Regardless, it is wrong because you would have to be returning a temporary (the current object should not be changed by operator+).

    Speaking of which, it should really be a non-member function anyway and can often easily be implemented in terms of operator+=.

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Quote Originally Posted by Daved
    I don't see how making it a reference would make chaining easier. Regardless, it is wrong because you would have to be returning a temporary (the current object should not be changed by operator+).
    I'm not disagreeing with you, but you do things enough, you kinda have to get out of the habit of it. I had to do that for a while.

    Anyway, from what I understand, operators that return the the type of the class is better overloaded as a member.
    Sent from my iPadŽ

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Do you remember why? I can't think of a reason, especially in this case where you are returning a temporary value anyway, not the actual object.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    I think that in the case of operator+, it probably is better to overload operator+= as a member function, then implement operator+ in terms of operator+= as a non-member non-friend function.

    It might be something like:
    Code:
    MyClass& MyClass::operator+=(const MyClass& rhs) {
    	// ...
    	return *this;
    }
    
    MyClass operator+(const MyClass& lhs, const MyClass& rhs) {
    	return MyClass(lhs) += rhs;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    It all boils down to habits and programming practice. For me, I'd implement functions which return myclass objects by reference as member functions (so I can use this* ), and all other functions as nonmember functions, possibly adding a friend declaration.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 01:14 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 07:38 AM
  4. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 12:55 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21