CMatrix:delete CMatrix:Cols

CMatrix:Copy


The Copy method duplicates the current CMatrix and returns the new object.

Syntax

M = CMatrix:Copy()

Remarks

This method copies an existing CMatrix and returns a reference to the new CMatrix. You cannot copy a CMatrix or other class object using the trivial statement B = A, as this simply makes the value of B a reference to the object A. In other words, B becomes an alias to A. To get around this, two copy methods are provided by the CMatrix class:

To create a CMatrix that is a copy of the current object, use the Copy method. This creates a new CMatrix B before duplicating the values of the current object A. For example,

 

  B = A:Copy()

-- CMatrix A already exists

To create a new CMatrix that is a copy of an existing object, use the copy constructor, as in

 

  B = CMatrix:new(A)

-- CMatrix A already exists

You can use the copying syntax that fits best with your script.

Example

The following script fragment duplicates the CMatrix M into a new CMatrix M2:

 

M = CMatrix:new()

-- create a CMatrix

M:Set( 2, 44, 1805 )

-- set member 2, 44

M:Set( 50, 20, 102 )

-- set member 50, 20

M:Set( 120, 2, 600.5 )

-- set member 120, 2

M2 = M:Copy()

-- creates a duplicate CMatrix M2

Printf("N = %d", M2:Count())

-- result: N = 3

M:delete()

-- if finished with M, clean up memory

Related Topics

CMatrix, new