CArray:Copy


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

Syntax

A = CArray:Copy()

where

    A is a new CArray containing the same values as the current one. In place of CArray, use the name of the actual object to be copied.

  

This method copies an existing CArray and returns a reference to the new CArray. You cannot copy a CArray 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 CArray class:

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

  B = A:Copy()

-- CArray A already exists

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

  B = CArray:new(A)

-- CArray A already exists

Choose the syntax that fits best with your script.

Example

The following script duplicates the CArray A into a new CArray A2:

A = CArray:new()

-- create a CArray

A:Set( 1, 12.55 )

-- set member 1

A:Set( 40, -155.23 )

-- set member 40

A2 = A:Copy()

-- creates a duplicate CArray A2

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

-- result: N = 2

A:delete()

-- if finished with A, clean up memory

Related Topics

new

CArray class