Mail Archives: djgpp/1997/09/03/12:49:55
// object.h
#ifndef OBJECT_H
#define OBJECT_H
#define BASE_OBJECT \
void (*DoSomething)();
typedef struct { BASE_OBJECT } Object;
typedef struct
{
BASE_OBJECT
int x;
}X_Object;
typedef struct
{
BASE_OBJECT
int y;
}Y_Object;
extern void InitializeObjects(void); //Declared in initobj.c
extern void Manip_X(X_Object *); //Declared in manip_x.c
extern void Manip_Y(Y_Object *); //Declared in manip_y.c
extern Object *ObjectList[]; //Declared in example.c
#endif
// eof object.h
// initobj.c
#include <stdlib.h>
/* Include file which contains all object functions and structures */
#include "object.h"
void InitializeObjects()
{
X_Object * x;
Y_Object * y;
if((x = malloc(sizeof(X_Object))) == NULL)
{
printf("Couldn't get memory\n");
exit(1);
}
x->DoSomething = Manip_X;
if((y = malloc(sizeof(Y_Object))) == NULL)
{
printf("Couldn't get memory\n");
exit(1);
}
y->DoSomething = Manip_Y;
ObjectList[0] = (Object*)x;
ObjectList[1] = (Object*)y;
}
// eof initobj.c
// manip_x.c
#include <stdio.h>
/* Include file which contains all object functions and structures */
#include "object.h"
void Manip_X(X_Object * x)
{
/* Set variable x from a X_Object to 1 */
x->x = 1;
/* Display the value x from the X_Object */
printf("X_Object int x = %d\n",x->x);
};
// eof manip_x.c
// manip_y.c
#include <stdio.h>
/* Include file which contains all object functions and structures */
#include "object.h"
void Manip_Y(Y_Object * y)
{
/* Set variable y from a Y_Object to 2 */
y->y = 2;
/* Display the value y from the Y_Object */
printf("Y_Object int y = %d\n",y->y);
}
// eof manip_x.c
// example.c
/* Include file which contains all object functions and structures */
#include "object.h"
/* Global list of basic objects */
Object *ObjectList[2];
int main()
{
int i;
/* Basic object pointer, that you will use to manipulate all objects */
Object * ObjectPtr;
/* Initialize all objects here */
InitializeObjects();
/* Example loop which manipalates all objects */
for( i = 0; i < 2; i++)
{
ObjectPtr = ObjectList[i];
ObjectPtr->DoSomething(ObjectPtr);
/* OUTPUT:
X_Object int x = 1
Y_Object int y = 2
*/
}
return 0;
}
// eof example.c
- Raw text -