Thread: Operator overloading and precedence

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 05: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, 06:19 PM
  5. precedence
    By modec in forum C Programming
    Replies: 3
    Last Post: 05-22-2003, 11:37 AM