Private inheritance of reference-counted objects

x::obj must be publicly inherited:

class ButtonRendererObj: virtual private x::obj {

// ...

};

This won't work because x::ref<ButtonRendererObj> needs to access the x::obj superclass. For the same reason, a reference-counted object cannot have non-public inheritance, either.

class ButtonRendererObj: virtual public x::obj {

// ...

};

class ButtonObj : private ButtonRendererObj {

// ...

	};

Explicitly inheriting from x::obj allows references to work, but with a caveat:

class ButtonRendererObj: virtual public x::obj {

// ...

public:
     void doButtonRender();

};

class ButtonObj : private ButtonRendererObj, virtual public x::obj {

// ...

	};

References to either class now works as expected, but the private inheritance isn't really private:

x::ptr<ButtonObj> buttonp;

// ...

buttonp->doButtonRender(); // doesn't work, private inheritance.


x::ref<ButtonObj>(x::ref<x::obj>(buttonp))->doButtonRender(); // this works

Both classes publicly inherit from x::obj, so a pointer or a reference to one can be dynamically casted through x::obj to the other one.

Careful usage of private inheritance, and friend declarations with x::ptr, allows truly private inheritance of reference-counted objects. But that adds extra complexity, and the basic approach strikes a reasonable balance. Private inheritance is voluntarily enforced, unless a long detour gets taken to work around it; and this back-door can be useful for out-of-band situations, like automated regression tests.