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

typedef unsigned long ULONG;
class CMatrix
{
public:
	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);
private:
	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.

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),
      _s_col(m._s_col),
      base_mat(m.base_mat)
{
    // 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.

Comments
quite explicit guide
This article has been dead for over six months. Start a new discussion instead.