CMatrix:new
The new method creates an instance of the CMatrix class. This method returns to the caller a reference to the object it creates. The calling code must assign this reference to a name (see below). If the object cannot be created, nil is returned.
Note: This method's name uses all lower case to present it as the analog of the C++ new operator. Generally, the names of class methods begin with an uppercase letter but new and delete are different.
M = CMatrix:new()
M = CMatrix:new( CMatrix2 )
M = CMatrix:new( nRows, nCols )
M = CMatrix:new( nRows, nCols, val )
where
If no argument
is passed, a new CMatrix object is created and contains no
values.
CMatrix2 is an existing CMatrix object whose
contents are copied into the new object.
nRows,nCols specify the number of rows and
columns in the new CMatrix.
val is the initial value assigned to the
nRows x nCols. If omitted, the matrix is filled with
value 0.0.
M is the new CMatix object returned. On
failure, nil is
returned.
The first example shows the default constructor and the use of delete to free memory when the CMatrix will no longer be used by the script:
|
-- create a new instance of M of the CMatrix class. |
-- |
-- other uses of the class go here, between new and delete. |
|
-- deletes the object and its associated memory. |
The next example shows the use of all 3 constructors: default, copy, and initialization:
|
-- initialization constructor |
|
-- result: M1 = 25 |
|
-- copy constructor |
|
-- result: M2 = 25 |
|
-- default constructor |
|
-- set the first member |
|
-- result: M3 = 1 |