CArray:Copy
The Copy method duplicates the current CArray and returns the new object.
A = CArray:Copy() |
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 |
You can use the copying syntax that fits best with your script.
The following script fragment duplicates the CArray A into a new CArray A2:
|
-- create a CArray |
|
-- set member 1 |
|
-- set member 40 |
|
-- creates a duplicate CArray A2 |
|
-- result: N = 2 |
|
-- if finished with A, clean up memory |