Operator overloading and precedence

This is a discussion on Operator overloading and precedence within the C++ Programming forums, part of the General Programming Boards category; I've been experimenting with some simple operator overloading. I have a simple class which contains an int. I've overloaded the ...

  1. #1
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303

    Operator overloading and precedence

    I've been experimenting with some simple operator overloading. I have a simple class which contains an int. I've overloaded the + operator to add two objects and it returns a new object:

    Code:
    Simple operator+(const Simple& rhs) {return Simple(n + rhs.n);}
    I've also overloaded the += operator and it returns a reference to the modified object:

    Code:
    Simple& operator +=(const Simple& rhs)
    {
        n += rhs.n;
        return *this;
    }
    And the << operator is overloaded like so:

    Code:
     friend ostream&
            operator<<(ostream& os, const Simple& cs)
    {
        return os << "Object: n = " << cs.n << endl;
    }
    My problem: I can do this....

    Code:
    Simple s1(15), s2(20);
    cout << s1 + s2;
    But if I do this...

    Code:
    cout << s1 += s2;
    I get the following error:

    Code:
    binary '+=' : 'std::ostream' does not define this operator 
    or a conversion to a type acceptable to the predefined operator
    However, if I use parenthesis, as in cout << (s1 += s2); it works fine, but I can't quite see why. Is it something to do with operator precedence?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    Quote Originally Posted by Sharke
    However, if I use parenthesis, as in cout << (s1 += s2); it works fine, but I can't quite see why. Is it something to do with operator precedence?
    Yes, the precedence of assignment operators is lower than that of shift operators, though the precedence of shift operators is lower than that of additive operators.
    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

  3. #3
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Aha, I see! And I guess that's why I should just throw parenthesis around things just to make sure...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C4554 Warning, check operator precedence for possible error
    By Bassquake in forum C++ Programming
    Replies: 2
    Last Post: 06-05-2008, 06:13 AM
  2. Replies: 15
    Last Post: 11-04-2006, 04:06 AM
  3. precedence / associativity
    By anthonye in forum C++ Programming
    Replies: 1
    Last Post: 11-06-2003, 11:03 AM
  4. Less than or equal - precedence in for loops
    By Idle in forum C Programming
    Replies: 4
    Last Post: 06-23-2003, 07:19 PM
  5. precedence
    By modec in forum C Programming
    Replies: 3
    Last Post: 05-22-2003, 12:37 PM

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