Construction and destruction

Another issue is the construction and destruction of objects. C does not have the operators new and delete. C has only malloc(), free(), and the related routines, so explicit routines for creating and initializing objects must be provided. Memory for the vtable of an object must be allocated and filled in correctly. The vtable memory must only be allocated once, no matter how many instances of the object get created. Free vtable memory only once, after all instances of the class are gone. Do this by allocating the vtables for all the classes in a POD as data for the PODSPod object itself, because only one instance of that object will ever exist and it will be destroyed last.

Although the complex problems of construction and destruction could occupy several pages, the PODS system is designed such that the base classes for objects a POD author will implement, do not require special construction or destruction. In fact, except for PODSPod, the base classes have no data members at all, and PODSPod has only a copy of a system-provided object pointer which does not need destruction in your code. So your only requirement is that you implement the destroy( ) method (inherited from PODSObject) in the proper way to undo what your <objectname> New did.

Your construction code must explicitly set up the vtable in your object. In general, there are only two places where a PODS object could be created in your POD: in PODSPodNew() and in methods of other objects, such as a PODSDocumentSrc or a PODSObjectSrc. In both cases, the techniques are basically boiler-plate copies of stereotypes, and are amply demonstrated in the PODS sample code. See PODS Code Samples.