D
Deniz Bahar
Hello,
A couple days ago my friend (OOP guy) shows me what OOP was all about
in C++. This morning I figured I can do pretty much the same thing
with C (by putting function pointers in structures and using Macros).
I've gone pretty far but I'm running into problems because the function
pointers within the structures need an extra argument (a pointer to
that type of structure) so they know which data to operate on. Also, I
have to initialize all the functions pointers for every structure
object I define.
I'm trying to get rid of all this using Macros, and this is where I
need help.
I get "invalid type argument ->" as error message from my
compiler/preprocessor.
Here are the includes and macros:
---------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define InitFunctions(P) {(P)->setname = nameset;
(P)->setlover = loverset;
(P)->setIQ = IQset;}
#define (P).NAME(S) (P).setname( (S), &(P) );
#define (P).LOVER(S, I) (P).setlover( (S), (I), &(P) );
#define (P).IQ(I) (P).setIQ( (I) , &(P) );
Here is my structure(simulated class) template:
-----------------------------------------------
struct PersonTag
{
char name[40];
char loves[20][40];
int IQ;
char *(*setname)(const char *, struct PersonTag *);
char *(*setlover)(const char *, int index, struct PersonTag *);
int (*setIQ)(const int, struct PersonTag *);
};
typedef struct PersonTag Person;
Here are the function definitions:
----------------------------------
char *nameset(const char *name, Person *P)
{
/* name = NULL signals read operation */
if(name)
strcpy(P->name, name);
return P->name;
}
char *loverset(const char *newlove, int index, Person *P)
{
/* newlove = NULL signals read operation */
if(newlove)
strcpy(P->loves[index], newlove);
return P->loves[index];
}
int IQset(const int IQ, Person *P)
{
/* IQ = 0 signals read operation */
if(IQ)
P->IQ = IQ;
return P->IQ;
}
Here is my main function:
-------------------------
int main(void)
{
Person Dan;
InitFunctions(Dan);
Dan.NAME("Dan");
Dan.LOVER("Julia", 0);
Dan.IQ(100);
printf("Name = %s\nFirst Lover = %s\n,IQ = %d\n",
Dan.NAME(NULL), Dan.LOVER(NULL,0), Dan.IQ(0) );
return 0;
}
I am not too worried about buffer overflow or error checking at this
point (strcpy). My difficulties are in trying to "hide away" the
operations of: setting function pointers in the structure objects to
the proper functions AND passing extra structure pointer argument every
time a function is called.
I would think this is possible because the name of the structure is
always used when I call a function, and it's seems pointless that I
keep having to repeat the name.
Examples:
Bob.setlover("Teresa" , &Bob);
Bob.setIQ(120, &Bob);
As you can see, "Bob" comes twice for each call. Can anyone help me
with my macros or point me to another solution? Thanks
A couple days ago my friend (OOP guy) shows me what OOP was all about
in C++. This morning I figured I can do pretty much the same thing
with C (by putting function pointers in structures and using Macros).
I've gone pretty far but I'm running into problems because the function
pointers within the structures need an extra argument (a pointer to
that type of structure) so they know which data to operate on. Also, I
have to initialize all the functions pointers for every structure
object I define.
I'm trying to get rid of all this using Macros, and this is where I
need help.
I get "invalid type argument ->" as error message from my
compiler/preprocessor.
Here are the includes and macros:
---------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define InitFunctions(P) {(P)->setname = nameset;
(P)->setlover = loverset;
(P)->setIQ = IQset;}
#define (P).NAME(S) (P).setname( (S), &(P) );
#define (P).LOVER(S, I) (P).setlover( (S), (I), &(P) );
#define (P).IQ(I) (P).setIQ( (I) , &(P) );
Here is my structure(simulated class) template:
-----------------------------------------------
struct PersonTag
{
char name[40];
char loves[20][40];
int IQ;
char *(*setname)(const char *, struct PersonTag *);
char *(*setlover)(const char *, int index, struct PersonTag *);
int (*setIQ)(const int, struct PersonTag *);
};
typedef struct PersonTag Person;
Here are the function definitions:
----------------------------------
char *nameset(const char *name, Person *P)
{
/* name = NULL signals read operation */
if(name)
strcpy(P->name, name);
return P->name;
}
char *loverset(const char *newlove, int index, Person *P)
{
/* newlove = NULL signals read operation */
if(newlove)
strcpy(P->loves[index], newlove);
return P->loves[index];
}
int IQset(const int IQ, Person *P)
{
/* IQ = 0 signals read operation */
if(IQ)
P->IQ = IQ;
return P->IQ;
}
Here is my main function:
-------------------------
int main(void)
{
Person Dan;
InitFunctions(Dan);
Dan.NAME("Dan");
Dan.LOVER("Julia", 0);
Dan.IQ(100);
printf("Name = %s\nFirst Lover = %s\n,IQ = %d\n",
Dan.NAME(NULL), Dan.LOVER(NULL,0), Dan.IQ(0) );
return 0;
}
I am not too worried about buffer overflow or error checking at this
point (strcpy). My difficulties are in trying to "hide away" the
operations of: setting function pointers in the structure objects to
the proper functions AND passing extra structure pointer argument every
time a function is called.
I would think this is possible because the name of the structure is
always used when I call a function, and it's seems pointless that I
keep having to repeat the name.
Examples:
Bob.setlover("Teresa" , &Bob);
Bob.setIQ(120, &Bob);
As you can see, "Bob" comes twice for each call. Can anyone help me
with my macros or point me to another solution? Thanks