= operator overloading

This is a discussion on = operator overloading within the C++ Programming forums, part of the General Programming Boards category; Hi, everyone, I overloaded 3 opearators in my Polynomial class, they are "*" which returns the product of two polynomials, ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    3

    = operator overloading

    Hi, everyone,

    I overloaded 3 opearators in my Polynomial class, they are

    "*" which returns the product of two polynomials, return type is Polynomial.
    "+" which return the sum of two polynomials, return type is Polynomial.
    "=" which makes the left side have a deep copy of the right side.

    But if I write sum = poly1 + poly2; in the main driver, the compiler gives me error saying illegle operand, i think it's for the "=" operand.
    If I get rid of +poly2, leaving sum = poly1, it works fine. I dont know why. The + sign should perform first, return a Polynomial object, and then the = operator make a deep copy of the return object to the left side sum.

    So how can I make sum = poly1 + poly2 to work? Thanks for the helps.

    Here is the code of the overloaded "="
    Code:
    Polynomial & Polynomial :: operator = (Polynomial & rightSide)
    {    
        if(head != NULL) // Empty the left side Polynomial frist if it's not empty
        {
            for(int i = 0; i < totalTerms; i++)
                deleteFirstNode(head);
        }
        
        rightSide.current = rightSide.head;
        
        for(int i = 0; i < rightSide.totalTerms; i++) // Make deep copy, create a copy of the linked list
        {
            (*this).addTerm(rightSide.current -> getCOEF(), rightSide.current -> getEXP());
            rightSide.current = rightSide.current -> getLink();
        }
        
        return *this;        
    }

  2. #2
    Registered User
    Join Date
    Jul 2007
    Location
    Kansas, USA
    Posts
    12
    I just had some trouble with overloading operators, and it seems like the following worked fine (there's probably an easier way to do it that I haven't figured out)
    Code:
    Polynomial& Polynomial::operator=(Polynomial& rightSide)
    {
       // ... rest of function ...
       Polynomial &ref_rightSide = rightSide;
    
       return ref_rightSide;
    }
    That might be something. Other than that, I don't know if I'd be able to help you without seeing some more code.

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Let's see operator + ()
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    The problem might be that your parameter is not a reference to const. When you use operator+ to add poly1 + poly2, the result is a temporary object. But you cannot pass a temporary object to a function that expects a non-const reference.

    The operator= parameter should be const Polynomial &.

    If that's not it, make sure to post text of the full error message(s).

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    The copy assignment operator should take a const reference, not a reference, as its parameter. When you make the change, I would expect a compile error from the "rightSide.current = rightSide.head;" statement. What you should do is create current as a local variable, and then use this local variable to traverse rightSide.

    I suggest ignoring aprescott_27's advice. Returning *this is correct behaviour for a copy assignment operator.

    Incidentally, notice that the part where you empty the *this Polynomial is similiar to the destructor, and the part where you make a deep copy is similiar to the copy constructor. You might want to implement the copy assignment operator in terms of the copy constructor, destructor, and a member swap function. The basic idea is to copy the right hand side object to a temporary, and then swap the *this object with the temporary, and let the destructor destroy the temporary that now has the old value of the *this object. A possible implementation would be:
    Code:
    Polynomial& Polynomial::Polynomial(const Polynomial& rhs)
    {
        if (this != &rhs)
        {
            Polynomial temp(rhs);
            swap(temp);
        }
        return *this;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    3
    Quote Originally Posted by laserlight View Post
    The copy assignment operator should take a const reference, not a reference, as its parameter. When you make the change, I would expect a compile error from the "rightSide.current = rightSide.head;" statement. What you should do is create current as a local variable, and then use this local variable to traverse rightSide.

    I suggest ignoring aprescott_27's advice. Returning *this is correct behaviour for a copy assignment operator.

    Incidentally, notice that the part where you empty the *this Polynomial is similiar to the destructor, and the part where you make a deep copy is similiar to the copy constructor. You might want to implement the copy assignment operator in terms of the copy constructor, destructor, and a member swap function. The basic idea is to copy the right hand side object to a temporary, and then swap the *this object with the temporary, and let the destructor destroy the temporary that now has the old value of the *this object. A possible implementation would be:
    Code:
    Polynomial& Polynomial::Polynomial(const Polynomial& rhs)
    {
        if (this != &rhs)
        {
            Polynomial temp(rhs);
            swap(temp);
        }
        return *this;
    }
    Thanks for the help. I fixed it using your suggestion. Thanks.

Popular pages Recent additions subscribe to a feed

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