Dilemma with "operator overloading"...

This is a discussion on Dilemma with "operator overloading"... within the C++ Programming forums, part of the General Programming Boards category; Hi all, let's say we have a class Integer and we want to be able to add some two Integer ...

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

    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:
    Code:
    class Integer
    {
    public:
        // Operator "+"
        const Integer
        operator+(const Integer& right) const
        {
            return Integer(i + right.i)
        }
    private:
        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)


    Thanks.

    Petike

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

    --
    Mats
    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
    Posts
    161
    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
    Posts
    8,893
    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:
    Code:
    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!
    CornedBee

    "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, 07:26 AM
  4. OpenGL and DevCPP: A dilemma for newbies
    By Sunny in forum Game Programming
    Replies: 12
    Last Post: 04-08-2005, 10:47 AM
  5. dilemma
    By axon in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 12-03-2003, 01:28 PM

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