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
ah, thx
Here is the variables in my classI 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.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; };
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.
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
how do I obtain param.M[x].*?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?*/ }
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.