CMatrix:Dup


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

Syntax

M = CMatrix:Dup()

where

    M is a new CMatrix containing the same values as the current one.

  

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 Dup method. This creates a new CMatrix B before duplicating the values of the current object A:

 

  B = A:Dup()

-- 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

Choose the syntax that fits best with your script.

Example

The following script duplicates the CMatrix Mold into a new CMatrix Mnew:

Mold = CMatrix:new()

-- create a new CMatrix

Mold:Set( 2, 44, 1805 )

-- set member 2, 44

Mold:Set( 50, 20, 102 )

-- set member 50, 20

Mold:Set( 120, 2, 600.5 )

-- set member 120, 2

Mnew = Mold:Dup()

-- create a duplicate M2

Printf("N= %d,%d", Mold:Count(), Mnew:Count())

-- result: N= 3,3

Related Topics

new

Copy

CMatrix Class