Thread: = operator overloading

  1. #1
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926

    = operator overloading

    hey,
    I am pretty new to C++ and am not sure on this.
    Code:
    SparseMatrix &SparseMatrix::operator=(const SparseMatrix &another)
    {
       /*stuff setting up the r,c, and val variables*/
       SparseMatrix retval(r,c,val);
       return retval;
    }
    However when I make an assignment ex:
    Code:
    transpose=test.naiveTranspose();
    transpose has all null values?
    What am I doing wrong?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    What are you doing with another?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Code:
    SparseMatrix &SparseMatrix::operator=(const SparseMatrix &another)
    {
       int r[100],c[100],x;
       double val[100];
       for(x=1;x<another.numTerms()+1;x++){
          r[x+1]=another.M[x].i;
          c[x+1]=another.M[x].j;
          val[x+1]=another.M[x].val;
       }
       r[0]=rowDim();
       c[0]=colDim();
       val[0]=numTerms();
       SparseMatrix retval(r,c,val);
       return retval;
    }

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Why all the temporaries in there? Don't you want to copy from another to the current object?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What am I doing wrong?
    You are returning a reference to a local variable, for starters.

    My suggestion is to implement the copy constructor (you should do that anyway if you want to implement the copy assignment operator), a swap member function, and then implement the copy assignment operator in terms of those two (and the destructor, which you probably also have to write).
    Code:
    SparseMatrix &SparseMatrix::operator=(const SparseMatrix &another)
    {
        if (this != &another)
        {
            SparseMatrix temp(another);
            swap(temp);
        }
        return *this;
    }
    Of course, now your problem would have shifted to implementing the copy constructor
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You only need to implement operator = yourself if the class has member variables that don't on their own already get deep copied (unless there is some intended logic like reference counting intended)
    So the first question is, do you even need to write it? To answer this, we need to see the definition of SparseMatrix, in particular the variable declarations inside it.
    Then assuming you do need to write it, this should then show us exactly how to write 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"

  7. #7

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    There's a good possibility laserlight knew that, as it is fine to have that check in there as long as it would also work just fine without it.
    http://www.gotw.ca/gotw/023.htm:
    4. While you can't rely on the "this != &other" test, there's nothing wrong with using it as an attempt to optimize away known self-assignments. If it works, you've saved yourself an assignment. If it doesn't, of course, your assignment operator should still be written in such a way that it's safe for self-assignment. There are arguments both for and against using this test as an optimization, but that's beyond the scope of this GotW.
    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"

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Unless you believe self assignment will happen often, it would be more efficient to just swap() without the check for self assignment.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Unless you believe self assignment will happen often, it would be more efficient to just swap() without the check for self assignment.
    I agree. On the other hand, I think that a check for self-assignment makes the implementation more robust and clear that we will not choke on self-assignment, the trade-off in efficiency being relatively small anyway.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by iMalc View Post
    You only need to implement operator = yourself if the class has member variables that don't on their own already get deep copied (unless there is some intended logic like reference counting intended)it.
    To nitpick:Or unless you have non-static const data members.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    It probably won't make a noticable difference whether the check is there or not, but I just like to keep things as efficient as possible (without jumping through hoops to get there). A nanosecond saved is a nanosecond earned.
    I guess if a lot of people on your team aren't quite up to speed on the full power of C++ (which I like to call C+ programmers), then I guess it would be OK to use more basic techniques like checking for self-assignment... But if all the developers on your team are at the same level of expertise, then there's no need to add extra code to spell it out for them.

  13. #13
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Quote Originally Posted by iMalc View Post
    4. While you can't rely on the "this != &other" test
    Why that, even meyers (first edition though) suggest it? is inheritance/polymorphy the reason?

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by King Mir View Post
    To nitpick:Or unless you have non-static const data members.
    Yes, or references - noted.

    Why that, even meyers (first edition though) suggest it? is inheritance/polymorphy the reason?
    I highly recommend reading the particular GotW and all the rest of them as well, and it should answer your question.
    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"

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Meh, use Boost's addressof macro instead of & if you're paranoid.
    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