ASL
|
Several classes implement reference counting so that objects are copied by reference (shared).
The object will be destroyed and freed when it no longer has references. For a separate copy of the object, there is a clone()
method.
All containers such as Array, Map, Dic, Stack, HashMap, are shared this way. String is currently not shared but might be in the future.
Other shared classes also support inheritance and polymorphism: Xml, XmlText, Socket, TlsSocket.
It works like this. Suppose we have a shared class Shape
and derived classes Circle
and Square
.
We can keep them in an array:
We can check if an object is actually of a derived class, and cast it dynamically:
For a non-owning pointer to the underlying object (e.g. to avoid circular references in a graph), these classes have a ptr()
method that returns a C::Ptr
class object.
We can check if two objects are the same (they point to the same memory):
Objects of these types are automatically constructed (initialized), as is usual in C++, and directly usable. If for some reason you need to create a non-initialized object and defer construction you can create the object with a null pointer argument.