Copy Constructors: References and Copies
In C++, it is common to construct a variable from an existing variable. For example, this occurs when returning a variable from a function, when passing a variable by value, or when creating a temporary in arithmetic expressions. This construction is accomplished by a special constructor called the copy constructor. When a Rogue Wave matrix is constructed by the copy constructor, the copy constructor causes the data in the new object to reference the data in the old object. This is done for efficiency, since it takes much longer to copy the data than to reference it. If you have used the Essential Math Module vector classes, you are familiar with this technique because the vector, matrix, and array classes all use it. The simplest way to illustrate this kind of referencing is with an example:
#include <rw/lapack/skewmat.h> // 1
#include <iostream>
int main()
{
RWSkewMat<double> S(3,3); // 2
S.zero();
RWSkewMat<double> R = S; // 3
RWSkewMat<double> T = S.copy(); // 4
R(1,2) = 5; // 5
T(2,1) = 3; // 6
std::cout << "R= " << R << std::endl; // 7
std::cout << "S= " << S << std::endl;
std::cout << "T= " << T << std::endl;
return 0;
}
For additional information on the technique described in this example, please see the Essential Math Module User’s Guide chapter on vectors, matrices, and arrays.