Objects

In this library, all reference-counted objects are virtually derived from the x::obj class:

#include <x/obj.H>

class buttonObj: virtual public x::obj {

// ...

public:

    buttonObj(int width, int height);
    ~buttonObj() noexcept;
};

//

#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. All classes derived from x::obj must be derived virtually. 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.

All classes derived from x::obj must explicitly define a destructor that throws no exceptions (the noexcept keyword). This is mandatory. Destructors should not throw exceptions anyway, this makes it explicit.