Thread: Function Pointer

  1. #1
    Registered User
    Join Date
    May 2004
    <<split from ancient thread:>>

    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 -

    //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.
    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?


  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    New York
    Kind of like dynamic binding for c. You could make your 'object' hold function pointers to different function to perform different functions, but you couldn't really wouldn't reap the benefits of inheritence or anything and wouldn't be able to have like collections because the types would seem unrelated. This c-faq entry explores some options:

    This is also a nice little publishing about object oriented c.

  3. #3
    Registered User
    Join Date
    May 2004
    Thanks Tonto, I'll check those out.

  4. #4
    Registered User
    Join Date
    May 2004
    After a quick look there's plenty to think about! That ooc doc will be a good read thanks for the link.

    Trying not to get to OO though I even thought of using a simple struct (the model) that holds an array of simple structs with a type name, a handle to its instance/record, and a pointer to a draw function to draw that type.
    Perhaps that would be easier?

  5. #5
    Registered User
    Join Date
    May 2004
    For those interested I did a bit of searching and between these two links I think I have enough tools to accomplish what I need without getting to deep in OO


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Function Pointer help
    By Skydt in forum C Programming
    Replies: 5
    Last Post: 12-02-2005, 09:13 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM