General notes (before you edited your question):
Just calling the copy assignment operator in a copy constructor is not a general solution. They mean different things (I mean, why should they be there if they meant the same).
The assignment operator is called when an (existing!) object is assigned a new value.
If this object is of the same type, we call this also the copy assignment, since a typical implementation simply copies the contents (but might share some referenced things, like for example shared pointers or PIMPL classes with shared data). A copy assignment operator is implemented by the compiler automatically unless you provide one. It copies each member using the assignment operators of their types (primitive members are also copied).
The copy constructor is called when a (not yet existing!) obejct is assigned an initial value of the same type, i.e. it should be initialized with the copy of an existing object. Again, if you don't provide a copy constructor, the compiler generates one for you, again just copying the members using the copy constructor.
If you call the assignment operator from within the copy constructor, this means that the generated program performs the following steps when copy-initializing a new object:
- (Unless you use member-initializer list:) It initializes non-primitive class members with default constructors. Primitive types are left uninitialized.
- Then, the assignment operator is called. If you didn't define one, it copies all members.
So it should be fine in most cases, but there are a couple of cases in which this does not work, in particular if your class has members which can't be default-constructed or can't be assigned. If this is the case, and if they can still be copy-constructed (in contrast to copy-assigned), you have to initialize the members in the member-initialization list of the copy constructor manually.
EDIT (Since the question got edited): In your case, data
is a primitive type (all pointers are considered primitive), so you have to initialize it properly in the copy constructor before calling your assignment operator. If you don't do so, the assignment will delete an uninitialized pointer. So the best you should to (to avoid code duplication; it could be more efficient if you did):
Array<T, ROW, COL>(const Array<T, ROW, COL> &array) :
data(0) // Now it is at least initialized, although inefficient
{
*this = array;
}
The assignment operator will then try to delete a null-pointer, which is okay (it just does nothing), and then performs the actual copy. Consider the data(0)
initialization just as a "default-constructed null Array<...>
" object, just temporarily. (Maybe you already provide a null-object which does not allocate external memory?)