New Arrays - Function vs. Constructor?

classic Classic list List threaded Threaded
2 messages Options
jrd
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

New Arrays - Function vs. Constructor?

jrd
Why does the book specify that the construction of a new Array should be done with the "function" program component and the following syntax:

function Array new(int size)

as opposed to using the "constructor" program component?

e.g., constructor Array new(int size)

* Like Strings and other objects, it appears that you create an individual instance of an Array and then dispose of it after use for proper memory/garbage collection?  That object programming is generally initiated with constructors, not functions.

However, this is the only example I've seen to the atypical use of "function" instead of "constructor" to create new objects?  Is it somehow because Arrays have no field variables of their own and therefore no need to set those as part of object creation?

Any thoughts?

- JRD
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: New Arrays - Function vs. Constructor?

cadet1620
Administrator
jrd wrote
Why does the book specify that the construction of a new Array should be done with the "function" program component and the following syntax:

function Array new(int size)

... Is it somehow because Arrays have no field variables of their own and therefore no need to set those as part of object creation?
Yes, that is exactly the case.

A constructor automatically allocates the correct sized memory block to hold the object's fields, so a constructor for Array would not allocate any storage to hold the Array elements.

By using a function masquerading as a constructor, Array can allocate the required variable sized memory block.

--Mark
Loading...