Previously I covered a simple constructor for OOPinC, and now I will cover a destructor. The purpose of a destructor is to free any resources associated with an instance of a class when it is no longer needed. If the class does not use instance-specific resources (e.g. heap memory, opened files, pipes, etc.), then the destructor is very simple:
void*
CLASS_destructor(CLASS *self)
{
/* Free your resources here */
return self;
}
Of course we'll need a replacement for the C++ delete operator:
#include <stdlib.h>
#define CLASS_destroy(p) \
free(CLASS_destructor(p))
There is one more form of initialization that most classes should have, and that is a special static initializer for the case where the instance resides in the static data segment. In my next post I will cover the static initializer, and then I'll start work on a useful string buffer class to eliminate those pesky buffer overflows that wreak all kinds of havoc on careless programmers ;-)
No comments:
Post a Comment