Each class has a public static TCIDClass object. This is generally accomplished via a macro that each class puts into its header, but it is sometimes manually provided in the base of template classes. The STDMEMBERS and STDMEMBERS2 macros handle this chore and appear at the bottom of every CIDLib class definition. STDMEMBERS2 also registers the class object with the type repository, allowing for dynamic creation of that type of object by name, at runtime. This requires that the class provide default construction, but it can be hidden and the factory function made its friend. All of the factory functions have name that is made up by the STDMEMBERS2 macro, but you can build it from the class name. Its pMakeNew_xxx() where xxx is the literal name of the class. It has no parms and returns a pointer to the root TCIDObject class (so that it can handle all CIDLib classes.) A prototype is in CIDLib_Type.Hpp, named pfnObjFactory, for your convenience but generally you should leave this up to the magic macros.
Here are some examples of the TCIDClass class in use. It is generally used in conjunction with some aspect of the RTTI system or the dynamic typing system, ect...
//
// Given a pointer to an object (via a pointer to the root CIDLib
// class so its as generic as possible), get the type of the object
// and display it.
//
TCIDObject pobjToUse = [got set at some point];
otfOut << "The object type is:" << pobjToUse.clsIsA() << _NewLn_;
//
// We can also get to the class object via the class name. Here, we
// use the static class object to create a new object of that. This
// is kind of stupid since we could have just constructed one, but
// this shows an occasionally useful technique.
//
// Note that we had to cast it back to the right type because this
// method returns a pointer to a base TCIDObject pointer. In general
// you would use the ::MakeNewOfType() template function to do it
// in a typesafe manner. It uses this underlying mechanism but can
// do some important grunt work checking for you.
//
TArea* pareaNew = (TArea*)TArea::clsThis.pobjMakeNew();
//
// Here is the typesafe way. It insures that the new type is derived
// from the type of the passed pointer. If so, it does the creation
// and casting, and fills in pareaNew with the new object pointer.
//
::MakeNewOfType(pareaNew, TArea::clsThis);
//
// We can also check whether one object is of a particular class
// or not. Or if its descended from some class.
//
if (pobjToUse->bIsA(TPoint::clsThis))
{
// Then do something
}
if (pobjToUse->bIsDescendantOf(TWindow::clsThis))
{
// Then do something
}
Default construction is provided as a convenience, but don't abuse it by passing around unitialized class objects.
TCIDClass();
TCIDClass
(
const tCIDLib::Tch* const pszClassName
, const tCIDLib::Tch* const pszUserName
, tCIDLib::pfnObjFactory pFactoryFunc = 0
);
TCIDClass
(
const TCIDClass& clsSrc
);
What would be bad would be to construct one for an existing class and not provide a factory function, or an incorrect one, ect... This ability is made available for you in order to support special needs for type registration but should not be abused.