Thread: Quick question regarding overloading operators

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    30

    Quick question regarding overloading operators

    Code:
    // vectors: overloading operators example
    #include <iostream>
    using namespace std;
    
    
    class CVector {
      public:
        int x,y;
        CVector () {};
        CVector (int,int);
        CVector operator + (CVector);
    };
    
    
    CVector::CVector (int a, int b) {
      x = a;
      y = b;
    }
    
    
    CVector CVector::operator+ (CVector param) {
      CVector temp;
      temp.x = x + param.x;
      temp.y = y + param.y;
      return (temp);
    }
    
    
    int main () {
      CVector a (3,1);
      CVector b (1,2);
      CVector c;
      c = a + b;
      cout << c.x << "," << c.y;
      return 0;
    }
    In the example above, on line #28, the object c is created. And c, without being assigned anything specific, can already use the defined overloaded operator function for the plus sign. How did the computer know to use the overloaded function for the object c? Does it check the class for any overloaded operators to see if it should use that by default instead of the normal operator for the plus sign?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    If you look carefully you will discover that CVector has a default constructor on line 9, so there is no real language reason why you couldn't use it in a statement such as c = a + b. In the future you might not want to define member functions inside of the class definition.

    It works because the changes are made to a copy of a, which is assigned to c.
    Last edited by whiteflags; 10-25-2012 at 08:34 PM.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Justin H View Post
    How did the computer know to use the overloaded function for the object c? Does it check the class for any overloaded operators to see if it should use that by default instead of the normal operator for the plus sign?
    Essentially, yes.

    Quote Originally Posted by whiteflags View Post
    It works because the changes are made to a copy of a, which is assigned to c.
    Huh, that's not what the code does. Neither a, nor the pass-by-value copy of a is modified.
    Perhaps the code was modified after you saw it? Afterall, line #28 has nothing on it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by iMalc View Post
    Huh, that's not what the code does. Neither a, nor the pass-by-value copy of a is modified.
    Perhaps the code was modified after you saw it? Afterall, line #28 has nothing on it.
    I didn't say anything about line 28, but it appears that my actual references are useless anyway. No idea why, I'm comfortable with blaming myself.

    Also, I explained this part pretty poorly I admit.
    temp.x = x + param.x;
    temp.y = y + param.y;

    So poorly it was wrong. I didn't bother fixing it because I didn't think it mattered.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about overloading operators
    By m37h0d in forum C++ Programming
    Replies: 15
    Last Post: 07-16-2008, 05:45 PM
  2. question about overloading operators
    By *DEAD* in forum C++ Programming
    Replies: 9
    Last Post: 05-08-2008, 10:27 AM
  3. Quick question on Boolean operators
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 10:43 PM
  4. Quick question about operator overloading
    By niudago in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2003, 08:03 AM
  5. Replies: 3
    Last Post: 01-22-2002, 12:25 PM