Thread: = operator overloading

  1. #16
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Quote Originally Posted by iMalc View Post
    I highly recommend reading the particular GotW and all the rest of them as well, and it should answer your question.
    your link is dead. Is the reason inheritance / polymorphy (I mean assignment of derived to base class for example)?

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The link merely has a superfluous : at the end. This one works:
    http://www.gotw.ca/gotw/023.htm
    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

  3. #18
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    ah, thx

  4. #19
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Here is the variables in my class
    Code:
    /*some methods*/
    SparseMatrix &operator=(const SparseMatrix& another);
    private:
       class Term;
       int nRows;
       int nCols;
       int nTerms;
       Term* M;
    };
    .
    .
    .
    /*nested class*/
    class SparseMatrix::Term
    {
    public:
      int i;
      int j;
      double val;
    };
    I understand that it is initializing to 0 because it is a local variable and losing it's memory once the function is complete, but I am not sure on the solution. I believe I have to use all of the temporaries because M is private and thus wouldn't I have to use a constructor with the temp variables instead? M is an array of points that include values ie adjacency matrix.

  5. #20
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Since M is a pointer, you need a copy constructor & assignment operator, otherwise the default functions that the compiler creates will only copy the pointer, not the data that it's pointing to.
    If the Term class also has pointers or other types that don't do deep copies, you should add copy constructors & assignment operators to all other classes that need them.

  6. #21
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I am not sure how to create a copy constructor because the member Term* M is private so I can not learn it's information. Here is what I have. The first constructor is what I already coded and
    Code:
    SparseMatrix::SparseMatrix(SparseMatrix param){
       SparseMatrix retval;
       int rows[100];
       int cols[100];
       double values[100];
    /*now I need to obtain param.M[x].i and param.M[x].j and param.M[x].val right?*/
    }
    how do I obtain param.M[x].*?

  7. #22
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That's not how a copy constructor works. Look up copy constructors and copy assignment operators in your book and remind yourself of what they are supposed to look like.

    Also, it doesn't matter that M is private, since you are in a SparseMatrix function you are allowed to access private member variables of a SparseMatrix object, even if it is a different object than the current one.

Popular pages Recent additions subscribe to a feed