Thread: Dilemma with "operator overloading"...

  1. #1
    Beginning game programmer Petike's Avatar
    Join Date
    Jan 2008

    Question Dilemma with "operator overloading"...

    Hi all,
    let's say we have a class Integer and we want to be able to add some two Integers (our objects) with operator "+" - so we overload the operator in our class:
    class Integer
        // Operator "+"
        const Integer
        operator+(const Integer& right) const
            return Integer(i + right.i)
        int i;   // Some integer
    But my question is:
    Why the return value in operator+ function is const? Should I understand it, that the return value cannot be ever changed?
    (Because in my opinion, from logic sight it doesn't get any meaning)



  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    I agree that it doesn't make much sense to make it const Integer.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2003
    I think "the return value cannot be ever changed" makes sense. Does "(a+b) = 7;" make sense to you? The "const" helps prevents people doing something like that by accident.

    I think you can make a plus sign return something you can change, and it may make sense to lists for example:

    newlist = (lista + listb).sort();

    I'm not sure if the compiler will actually let you do that though. If the lists are big I wouldn't recommend doing it that way.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Should I understand it, that the return value cannot be ever changed?
    While this is true, it only applies to the temporary being returned. You'll usually then assign the value to a non-const Integer.

    By the way, the canonical way of overloading the binary operators is to do this:
    class whatever
      whatever& operator +=(const whatever &rhs) {
        // Perform += here, i.e. add rhs's value to this one.
        return *this;
    (const?) whatever operator +(const whatever &lhs, const whatever &rhs)
      whatever tmp(lhs);
      tmp += rhs;
      return tmp;
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. High Scores - A Dilemma
    By CrazyNorman in forum Game Programming
    Replies: 5
    Last Post: 12-28-2006, 09:01 PM
  2. Struct dilemma
    By Ash1981 in forum C Programming
    Replies: 2
    Last Post: 01-06-2006, 05:19 AM
  3. Slight dilemma...
    By Junior89 in forum C++ Programming
    Replies: 6
    Last Post: 10-06-2005, 06:26 AM
  4. OpenGL and DevCPP: A dilemma for newbies
    By Sunny in forum Game Programming
    Replies: 12
    Last Post: 04-08-2005, 09:47 AM
  5. dilemma
    By axon in forum A Brief History of
    Replies: 6
    Last Post: 12-03-2003, 01:28 PM