<<split from ancient thread: https://cboard.cprogramming.com/showthread.php?t=6761>>
After a search this was the closest thread to what I'm trying to do.
Is it possible to have a function pointer in a struct that takes the same struct as it is in??
What I need it for is for a opengl app to draw objects of different types without having to use a switch to decide which drawing code to use (like a member function in C++ that calls it's own draw() routine).
eg. say I have a struct like so that is a line object that holds 2 point structs (start and end) and a draw function that uses the struct info to draw itself -
Code:
//line drawing function:
int DrawLine (Line linerecord)
{
gldrawline(linerecord);//or similar, only example.
return 0;
}
//typedef for function pointer:
typedef int (*PTR_TO_DRAW_FUNC)(Line);
// Line object struct:
typedef struct {
POINT start;
POINT end;
PTR_TO_DRAW_FUNC DrawLine;// calls 'this' drawline function.
}Line;
Then somewhere in my program I want to loop through my 'model' struct which holds records of different types of entities (circles lines etc) and calls the correct draw function for each type.
Would this work and is this the right way to go about it?
Thanks,
Mick.