= operator overloading

This is a discussion on = operator overloading within the C++ Programming forums, part of the General Programming Boards category; hey, I am pretty new to C++ and am not sure on this. Code: SparseMatrix &SparseMatrix::operator=(const SparseMatrix &another) { /*stuff ...

  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,006
    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,006
    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
    21,589
    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
    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
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    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,299
    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
    21,589
    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.
    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

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    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,299
    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,893
    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

Page 1 of 2 12 LastLast
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