Example:
class ListElem { int value; ListElem *next; }; ListElem *p; p = new ListElem; |
Syntax:
new TYPE[ #elements ]; |
The new operator returns the address of the first variable of the new array
NOTE:
You should store the
return value
in a variable of the type:
|
Example:
double *x; x = new double[100]; |
class Matrix3x3 { public: double A[3][3]; .... }; |
class Matrix { public: int M; // # rows int N; // # columns double *A; // matrix elements .... }; |
class MatrixNxN { public: int N; double *A; /* ------------------------------------------ CreateMatrix(n): reserve space for matrix ------------------------------------------ */ void CreateMatrix(int n) { A = new double[n*n]; // Reserve space for matrix N = n; // We must remember dimension // in order to access A[i][j] } } |
int main(int argc, char *argv[]) { int N; MatrixNxN m1; N = 4; // Remember the dimension m1.CreateMatrix(4); ... } |
|
A constructor function/method (or constructor for short), is a special type of member function that is (only) invoked when a class variable is defined |
MatrixNxN m1; or MatrixNxN m2(5); |
besides reserving the necessary memory to store the variable, a special constructor function will be invoked as well.
|
Example:
class MatrixNxN { public: double *A; int N; /* ------------------------------------------ Constructor 1: does not create a matrix ------------------------------------------ */ MatrixNxN() { A = NULL; N = 0; } /* ------------------------------------------ Constructor 2: creates a nxn matrix ------------------------------------------ */ MatrixNxN(int n) { A = new double[n*n]; N = n; } ... }; |
The way that C++ determine which constructor to invoke is by the number and type of parameters specified with the variable definition |
Example:
MatrixNxN m1; // Invokes Constructor 1 (no parameter) MatrixNxN m2(4); // Invokes Constructor 2 (int) MatrixNxN m3[4]; // Invokes Constructor 1 four times MatrixNxN m4[4] = {MatrixNxN(2), MatrixNxN(3), MatrixNxN(4), MatrixNxN(5)}; // Invokes Constructor 2 four times |
|
class className { public: className() { ... // code for default constructor } ... }; |
className var1; className var2[5]; |
class className { public: className( className x ) { ... // code instruct C++ to initialize // variable using parameter x } ... } |
This constructor is known as the copy constructor
className a; className b = a; // initialize b using a or className b(a); // initialize b using a |
The following example show a common error when copying arrays (matrices)
class MatrixNxN { public: double *A; int N; // **** BAD copy constructor for a Matrix !!! MatrixNxN( MatrixNxN & a) { A = a.A; // Only copied the address !!! N = a.N; } ... }; |
This constructor does NOT make another copy of the matrix
Instead, it make another copy of the address of the existing matrix
The following example show how to copy arrays (matrices)
class MatrixNxN { public: double *A; int N; // The right way to copy a Matrix MatrixNxN( MatrixNxN & a) { int i, j; A = new double(a.N * a.N); // Make a new array; N = a.N; for (i = 0; i < N; i = i + 1) for (j = 0; j < N; j = j + 1) A[i*N+j] = a.A[i*N+j]; // Copy elements } ... }; |
|
If we define a MatrixNxN variable in some scope, and later exits that scope , then some reserved memory have not been "un-reserved" !!! |
Example:
void f() { MatrixNxN m(4); // Allocate space for: // m.A // m.N // A[4*4] (using new in constructor) ... } // Deallocate space for: // m.A // m.N |
class MatrixNxN { double *A; int N; /* ---------------------------------------------- FreeMatrix(): free reserve space for matrix ---------------------------------------------- */ void FreeMatrix() { delete A; } ... (other stuff) } |
void f() { MatrixNxN m(4); ... m.FreeMatrix(); // Deallocate space used for A[4*4] } |
If you forget or does not know about it , then the program may run out of memory and crash...
class MatrixNxN { public: double *A; int N; ~MatrixNxN() { if ( A != NULL ) { delete A; } } ... } |
When you use a new operator in a constructor (to allocate space for some variable), you must provide a destructor to de-allocate the space allocated by the new operator |