Thread: Overloading Operator+...

  1. #1
    Ecologist
    Join Date
    Aug 2001
    Location
    Utah.
    Posts
    1,291

    Overloading Operator+...

    I'm confused by this... In the book I'm reading, it shows that
    this takes a parameter of a reference... But why? I don't understand
    how this works.

    Code:
    cOBJECT cOBJECT :: operator + (const cOBJECT &r){ 
      return cOBJECT(value + r.GetValue());
    }
    
    cOBJECT varone(3);
    cOBJECT vartwo(2);
    cOBJECT varthree;
    
    varthree = varone + vartwo;
    Will someone explain to me how this works? Does vartwo
    automatically get passed through this parameter when it's
    added to varone?

    Another question, exactly which variable is '+' being applied to?
    Which variable's overloaded-operator-function is being called?

    I'm very confused...
    Staying away from General.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Normally, if an operator like '+' is called when 1 or more operands is a class, then the compiler looks for suitable overloads of operator+().

    You can use operator+ just like the built in operator, but your compiler treats it just like any other function call. Like so

    Code:
    #include <iostream>
    
    class foobar{
    	int m_n;
    public:
    	foobar(int n):m_n(n){}
    	foobar operator+(const foobar& rhs){
    		return foobar(rhs.m_n + m_n);
    	}
    	void Print()const {std::cout << m_n << std::endl;}
    };
    
    
                        
    int main(void){
    
    	foobar a(10);
    	
    	foobar b(20);
    	
    	foobar c = a + b;//Expected way
    	
    	foobar d = b.operator+(c);//How compiler implements it
    	
    	c.Print();
    	
    	d.Print();	
    	
    }

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    overloading eh?

    Code:
    -----1)-------2)-----3)------4)-------------5)
    cOBJECT cOBJECT :: operator + (const cOBJECT &r)
    1) The return type.. could be int, void, double,string,etc. in this case the function will return a cOBJECT

    2) The class that this function belongs to.. in this case class cOBJECT

    3) Binary scope resolution.. or something like that... tells the compiler that operator+() function is inside the class cOBJECT

    4) The function name.. it could be called addNums.. it doesn't matter...

    5) The variable being passed in... const means it cannot be modified in any way...

    varthree = varone + vartwo;

    varone is calling the function + and it is passing in the variable vartwo which corresponds to #5 above.

    if the function were named addNums() instead of operator + in this case you would call it by varone.addNums(vartwo);

    Does vartwo
    automatically get passed through this parameter when it's
    added to varone?


    just remember in order to invoke the + function :
    leftside of + must be of type cOBJECT..
    rightside of + must be of type cOBJECT..

    both varone and vartwo are being passed into the function.. however.. inside the function to change a member of varone you just need to:

    v=15;

    but for vartwo u must write:

    vartwo.v=15;

    Code:
    class cOBJECT
      {
        public:
                  cOBJECT operator + (const cOBJECT &)
        private:
                  int v;
      }
    hope this mess helps u out in some way..
    Last edited by tegwin; 11-12-2002 at 06:29 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  4. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 12:55 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM