Stupid operator overloading question.

This is a discussion on Stupid operator overloading question. within the C++ Programming forums, part of the General Programming Boards category; For some reason I can't figure out the + operator. I can do += just fine, but when I try ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    28

    Stupid operator overloading question.

    For some reason I can't figure out the + operator. I can do += just fine, but when I try to do +, I keep getting the address of the object instead of the value. The program is dealing with fractions and simplifying them with Euclid's algorithm (to explain the class name). The code:

    Basic class definition:

    Code:
    class Euclid
    {
    public:
    //bunch of functions
    ...
    private:
    	int numerator;
    	int denominator;
    	int simplify(int, int);
    };
    This is +=, which works perfectly:
    Code:
    Euclid &Euclid::operator +=(Euclid &fract)
    {
    	numerator  = ((numerator * fract.denominator) + (denominator * fract.numerator));
    	
    	denominator = (denominator * fract.denominator);
    
    	int gcd = simplify(numerator, denominator);
    
    	numerator = (numerator)/gcd;
    	denominator = (denominator)/gcd;
    
    	return *this;
    }
    This is +, which sucks:
    Code:
    Euclid &Euclid::operator +(Euclid &fract)
    {
    	Euclid temp(1,1);
    	
    	temp.numerator  = ((numerator * fract.denominator) + (denominator * fract.numerator));
    	
    	temp.denominator = (denominator * fract.denominator);
    
    	int gcd = simplify(temp.numerator, temp.denominator);
    
    	temp.numerator = (temp.numerator)/gcd;
    	temp.denominator = (temp.denominator)/gcd;
    
    	return temp;
    }
    And this is how they are being used in the actual program:
    Code:
    	cout<<"Adding 1/2 to this fraction..."<<endl;
    	test = userfrac + half;
    	cout<<"The reduced fraction is "<< test.getnum() <<"/"<< test.getdem() <<"."<<endl;

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    The + operator shouldn't return a reference to an object. It takes two objects and returns the sum without altering either one. As in your example this is done by creating temp to hold the result. This variable is local to your method, and will go out of scope when the function returns. You need to return the object by value.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    28
    Haha, that was dumb. Thanks for the help, sorry for wasting your time though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Question Probably
    By Kyrin in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 12:51 AM
  2. Replies: 7
    Last Post: 11-03-2005, 11:17 PM
  3. Stupid Question
    By digdug4life in forum C++ Programming
    Replies: 22
    Last Post: 05-17-2005, 11:43 AM
  4. stupid, stupid question
    By xelitex in forum C++ Programming
    Replies: 5
    Last Post: 12-22-2004, 07:22 PM
  5. Stupid question: What does Debugger do?
    By napkin111 in forum C++ Programming
    Replies: 6
    Last Post: 05-02-2002, 10:00 PM

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