The new operator is used whenever a new instance (a new object) of a particular class should be created at the runtime of the application.
In its first version, the new operator expects the full name of the class to create the object. The class is thus well known already at the compilation time, and, it can't change at the runtime. Typically, you use this operator to dynamically create and show GUI components in response to e.g. user interactions. Assuming, you have in your project an Application::MessagePanel component implemented, then the following code would create a new instance of this panel and display it modal on the screen:
// Create a new instance of the message panel var Application::MessagePanel panel = new Application::MessagePanel; // Configure the panel. For example, determine the text to display in // its caption and message area, etc. panel.Caption = "WARNING!"; panel.Message = "Do you really want to delete the movie?"; // Show the panel on the screen and make it modal rootthis.Add( panel, 0 ); rootthis.BeginModal( panel );
In the second version of the new operator, the class of the object to create is determined by the value of a class operand. The class is thus unknown until the operand has been evaluated at the runtime. Moreover, during the lifetime of the application the operand can result in very different classes. As such the Chora compiler can't make any assumption regarding the actual class. The resulting data type of this operator is thus a generic object. Please note, if the operand is a null class, the new operator will result in a null value. For example:
var class c1 = ... // Some class var class c2 = null; var object result = new c1; // result = a new object of the class stored in 'c1' var object result = new c2; // result = null
This version of the new operator is ideal to implement object factories. Assuming, in your component you have a property called ItemClass defined with class as data type, then the following code will create an instance of the class currently stored in this property. In other words, the value of the property ItemClass controls which objects are created:
var object item = new ItemClass;
The new operator takes care of the memory allocation for the new object and its correct construction. It starts with the initialization of all variables, arrays, properties and embedded objects defined within the class. Next, the class own Init() constructor is invoked in context of the new object allowing the object to finalize its initialization. Please note, by derived classes, the base class will perform its initialization before derived class does it.
When a dynamically created object is not in use anymore, it is de-initialized and the occupied memory is freed automatically by Chora own Garbage Collector. An object is considered as not in use, if there is no way to access the object - directly or indirectly through members of other objects. Thanks to the Garbage Collector, you as programmer don't need to be concerned with destroying unused objects.