My strategy is:
- Define all code for the class in a separate file
- Define all interfaces for the class in a separate header file
- All member functions take a "ClassHandle" which stands in for the instance name (instead of o.foo(), call foo(oHandle)
- The constructor is replaced with a function void ClassInit(ClassHandle h, int x, int y,...) OR ClassHandle ClassInit(int x, int y,...) depending on the memory allocation strategy
- All member variables are store as a member of a static struct in the class file, encapsulating it in the file, preventing outside files from accessing it
- The objects are stored in an array of the static struct above, with predefined handles (visible in the interface) or a fixed limit of objects that can be instantiated
- If useful, the class can contain public functions that will loop through the array and call the functions of all the instantiated objects (RunAll() calls each Run(oHandle)
- A Deinit(ClassHandle h) function frees the allocated memory (array index) in the dynamic allocation strategy
Does anyone see any problems, holes, potential pitfalls or hidden benefits/drawbacks to either variation of this approach? If I am reinventing a design method (and I assume I must be), can you point me to the name of it?