I have implemented my first C++ class as following thanks to the generous help of Ancient Dragon:

typedef unsigned long ULONG;
class CMatrix
	CMatrix(const CMatrix& m);
	CMatrix(size_t row, size_t col);
	ULONG sum();                                         
	size_t s_row();                                     
	size_t s_col();                                  
	CMatrix& operator *= (const CMatrix& m);
	size_t _s_row;
	size_t _s_col;
	vector< vector<short> > base_mat;   // for storing the elements the matrix.

The trouble now is, I can design the function body of

CMatrix(size_t row, size_t col);

, but have no opinion about the function body of

CMatrix(const CMatrix& m);

, since the CMatrix class has several members, the function seems not to be as easy as an operation collection of assignments. Any one can give me some advice? thank u in advance.

7 Years
Discussion Span
Last Post by Tom Gunn

If all of the fields have an overloaded copy constructor or assignment operator, or if they are built in types, you can just do assignments of all fields and everything should work right:

// use copy constructors and initialization
CMatrix::CMatrix(const CMatrix& m)
    : _s_row(m._s_row),
    // all work in initializer list
// use assignment
CMatrix::CMatrix(const CMatrix& m)
    _s_row = m._s_row;
    _s_col = m._s_col;
    base_mat = m.base_mat;

The first is recommended because it tends to be more efficient to initialize to what you want instead of default initializing and then assigning what you want.

Votes + Comments
quite explicit guide
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.