NAME
@new - Create a new instance of a class
SYNOPSIS
@new {CLASS-NAME}
DESCRIPTION
The @new operator is used to create a new instance of a class.
It initializes the object, and calls its constructor (if it exists). The directive expands to the address of the newly created object.
EXAMPLE
@MyClass* myObject=@new MyClass
shell_variable=@new MyClass
echo @new MyClass
NOTES
EXPANSION
The @new directive expands to the address of the newly created object. This address is then interpreted according to the shell context in which the directive is used. For example, if the directive is used in a context where a command is expected, the result will be treated as a command. If it is used in a context where an argument is expected, the result will be treated as an argument.
Most commonly, you’ll want to assign the result of @new to a pointer.
echo @new Object # Passes the address of the new object as an argument to 'echo'
@Object* obj=@new Object # This assigns the address of the new object to a pointer
@new Object # Watch out! This attempts to run the address of the new object as a command; you'll probably see "command not found."
LIFETIME
Objects created with @new are not automatically deleted when they go out of scope. Unlike objects which are instantiated by @ClassName objectName, objects created with @new must be explicitly deleted using the @delete directive.
The object’s constructor will be called before the pointer is returned. This means that the object will be fully initialized before anything can use it.
SEE ALSO
- bpp-delete(3) for deleting an object
- bpp-classes(3) for more information on classes