From: "Mr Jinks" Newsgroups: comp.os.msdos.djgpp Subject: Re: Functions in struct's... possible? How? Date: 3 Sep 1997 14:23:24 GMT Organization: World Online Message-ID: <01bcb874$f70442e0$b986f1c3@lodewijk> References: <33FCDA5C DOT 2353659F AT execulink DOT com> <5tippg$ci7$2 AT news DOT sendit DOT nodak DOT edu> <5tkq9a$2se$1 AT helios DOT crest DOT nt DOT com> <5tmcai$nuo$1 AT news DOT sendit DOT nodak DOT edu> <340B1E27 DOT 44FAD207 AT alcyone DOT com> <01bcb756$1d6d3ce0$5386f1c3 AT lodewijk> <5uisg2$s96 AT freenet-news DOT carleton DOT ca> NNTP-Posting-Host: dlft1-p185.worldonline.nl Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Lines: 132 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk // 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 /* 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 /* 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 /* 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