CNCL requires that all classes have a common set of member functions available. These functions provide runtime type checking and type information, creation of objects via class descriptions, and safe type casts.
The common member functions are:
CNClassDesc CLASS::class_desc() const;
bool CLASS::is_a(CNClassDesc desc) const;
TRUE
if
the queried object is type compatible with class desc
.
void CLASS::print(ostream &strm = cout) const;
void CLASS::dump(ostream &strm = cout) const;
strm
.
The function print()
is defined in greater detail in the derived
classes. The function dump()
is intended for debug purposes.
The functions above are defined as pure virtual functions in the
top-level class CNObject
. They are required in every derived class.
Furthermore the following static member functions are required to allow object creation via the class description and safe type casts:
CLASS* CLASS::cast_from_object(CNObject *obj);
(CLASS *)
type cast. It checks on
type compatibility between the object passed with the obj
pointer
and CLASS. If this is a not true, an error message is printed and the
program terminates.
The type checking may be disabled by defining the preprocessor macro
NO_TYPE_CHECK
, e.g. by supplying -DNO_TYPE_CHECK
on the
compiler's command line.
Example:
XYZ *px; // CNClass XYZ derived from CNObject ABC *pa; // CNClass ABC derived from CNObject DEF *pd; // CNClass DEF derived from ABC CNObject *po; po = new DEF; // Type compatible (C++ standard) pd = (DEF *)po; // The traditional way pd = DEF::cast_from_object(po); // The CNCL way pa = new DEF; pd = DEF::cast_from_object(pa); // o.k. px = XYZ::cast_from_object(pa); // Error
CNObject *CLASS::new_object(CNParam *param = NIL);
CNClass
to create new objects.
Every class in CNCL requires a class description object and a pointer
constant pointing to this class description. Following the CNCL
convention, the description object is named CLASS_desc
and
the pointer is named CN_
CLASS.
Example:
// Describing object for class XYZ static CNClass XYZ_desc("XYZ", "$Revision: 0.45 $", XYZ::new_object); // "Type" for type checking functions CNClassDesc CN_XYZ = &XYZ_desc;
Go to the first, previous, next, last section, table of contents.