- Copy rules
- Case of arrays when copying class instances
- Specific extension attributes
- The Clone function
Variables are copied using the "=" operator.
|Type of variables||Effect|
|Simple types (integer, real, string, etc.)||The value of the variable is copied. |
|Arrays||The destination array is a reference to the source array.|
|Associative arrays||The content of the array is copied.|
|Queue||The content of the queue is copied.|
|Stack||The content of the stack is copied.|
|List||The content of the list is copied.|
|Object = Dynamic object||The members of the dynamic object are copied to the members of the object.|
|Object = Object||The members of the source object are copied to the members of the destination object.|
|Dynamic object = Dynamic object||The destination dynamic object is a reference to the source dynamic object.|
|Dynamic object = Object||The destination dynamic object is a reference to the source object.|
|Structure = Dynamic structure||A copy of the structure is performed. Members with the same name are initialized with the existing values. Non-existing members are ignored. |
|Structure = Structure||A copy of the structure is performed. Members with the same name are initialized with the existing values. Non-existing members are ignored. |
|Dynamic structure = Dynamic structure||The destination dynamic structure is a reference to the source dynamic structure.|
|Dynamic structure = Structure||The destination dynamic structure is a reference to the source structure. |
|Advanced type = Advanced type||The advanced type variable is copied. Properties of the same name are initialized with the existing values. |
|Advanced type = Dynamic advanced type||The advanced type variable is copied. Properties of the same name are initialized with the existing values. |
|Dynamic advanced type = Advanced type||The destination dynamic advanced variable is a reference to the source advanced variable.|
|Dynamic advanced type = Dynamic advanced type||The destination dynamic advanced variable is a reference to the source dynamic advanced variable.|
Case of arrays when copying class instances
When copying class instances, all the members of the class are copied to the new instance, including the arrays. Thus, arrays are independent in all class instances.
To prevent getting independent arrays in all class instances:
- Open the project description window: on the "Project" tab, in the "Project" group, click "Description".
- In the "Compilation" tab, uncheck "Arrays: the assignment copies the content".
For more details, see Project description, compilation tab
Specific extension attributes
Two specific extension attributes can be used to indicate the operations to be performed:
- <Copy=Clone>: when used on a dynamic object, this extension attribute allows forcing the cloning of the object.
- <Copy=False>: when used on a member, this extension attribute prevents copying the member's value when copying an object to another object.
Clone is used to construct a copy of a class instance:
- An object of the actual class of the class instance is allocated.
- The members of the class instance are copied to the members of the new object. The "Constructor" and "Destructor" methods are also copied.
Benefit: In the case of polymorphism, there is no longer need to write the code necessary for this cloning. Likewise, there is no need to write virtual methods in all derived classes and constructors.
Remark: The copy takes into account the <Copy> attribute of each member:
- <Copy=False> to ignore a member,
- <Copy=Clone> to clone.
// There is a traffic light in North Phoenix, at 4725 E. Mayo Blvd.
// It is red
StopLight is TrafficLight
StopLight.Address = "4725 E. Mayo Blvd., Phoenix, AZ"
StopLight.Status = DarkRed
// Another traffic light must be set at 5599 on the same boulevard
// If the object is simply copied...
CopyLight is dynamic TrafficLight = StopLight
CopyLight.Address = "5599 E. Mayo Blvd., Phoenix, AZ"
// The address of StopLight has also changed...!
// If the object is cloned...
CloneLight is dynamic TrafficLight = Clone(StopLight)
// CloneLight contains the same information as StopLight
// But if one of it properties is modified, it does not affect the original traffic light
// Change the address
CloneLight.Address = "5599 E. Mayo Blvd., Phoenix, AZ"
// >> StopLight.Address = "4725 E. Mayo Blvd., Phoenix, AZ"
CloneLight.Status = DarkRed
// >> StopLight.Status = DarkGreen
This page is also available for…