Structures as Pointers


In many programming languages, the term pointer signifies that multiple variables can all share the same memory space. With pointers, the variables may have different names, but the values will always be the same because they refer to the same place in memory. ColdFusion stores all structures as pointers.

Although ColdFusion's structure storage does not effect most ColdFusion templates, there are significant consequences when structures are copied because what is made is not actually a copy, but a new way to point to the same values.

Look at the following example:

 <cfset user.FirstName="Ben"> <cfset user.LastName="Forta"> <cfset user.age="21"> <cfset user.email="ben@forta.com"> <cfset usercopy=user> <cfset usercopy.age="22"> 

The above code snippet creates a structure named user, places several keys in it, then copies it using a <cfset> tag and changes the age. Although user.age should be 21 and usercopy.age should be 22, if you were to execute the code, you'd find out that when usercopy.age changed, user.age changed too. This is because user and usercopy actually both refer to the same structure internally.

To create an entirely new structure based on the original, use the Duplicate() function.



Macromedia ColdFusion MX 7 Certified Developer Study Guide
Macromedia ColdFusion MX 7 Certified Developer Study Guide
ISBN: 0321330110
EAN: 2147483647
Year: 2004
Pages: 389
Authors: Ben Forta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net