LibCXX virtually derives all reference-counted
objects from the
x::obj
class:
#include <x/obj.H> class buttonObj: virtual public x::obj { // ... public: buttonObj(int width, int height); ~buttonObj(); }; // #include <x/ref.H> #include <x/ptr.H> typedef x::ref<buttonObj> button; typedef x::ptr<buttonObj> buttonptr; typedef x::const_ref<buttonObj> const_button; typedef x::const_ptr<buttonObj> const_buttonptr; button okButton=button::create(100, 100);
x::obj
is the virtual superclass of all
reference-counted objects. Objects derived from
x::obj
must always be allocated from the
heap using create
() (described later).
create
() returns a
reference pointer to the newly-constructed
class instance, either x::ref
, or an x::ptr
, as described later.
Once allocated on the heap, reference-counted objects do not get copied.
x::obj
does not implement a copy constructor
or the assignment operator.
Subclasses of x::obj
may define their own
copy constructors and assignment operators, whose scope is limited to
the subclass.
By convention, names of classes derived from
x::obj
end with “Obj”.
x::obj
maintains an internal counter tracking
all outstanding references to the object.
x::obj
must use virtual inheritance.
This is required to correctly implement multiple inheritance.
Inheriting from multiple
x::obj
-derived classes ends up with a single
instance of the virtual x::obj
superclass of
the derived class, and a single counter, that properly tracks all
references to the derived object.