dynamic binding

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • aarklon@gmail.com

    dynamic binding

    Hi all,

    recently a friend asked me is there any dynamic binding in C...??
    to which i answered AFAIK it is in C++ only,
    but he says it is valid in C.

    if dynamic can be implemented via function pointers in C ,
    can anyone give an example for dynamic binding in C...??
  • Malcolm McLean

    #2
    Re: dynamic binding


    <aarklon@gmail. comwrote in message
    news:9382c71b-d16b-43c6-af71-3fe3e05f5737@e2 5g2000prg.googl egroups.com...
    Hi all,
    >
    recently a friend asked me is there any dynamic binding in C...??
    to which i answered AFAIK it is in C++ only,
    but he says it is valid in C.
    >
    if dynamic can be implemented via function pointers in C ,
    can anyone give an example for dynamic binding in C...??
    >
    typedef struct
    {
    void (*getxy)(void *ptr, double t, int *x, int *y);
    int (*getlength)(vo id *ptr);
    } LINE;

    typedef struct
    {
    int x1;
    int y1;
    int x2;
    int y2;
    } STRAIGHTLINE;

    typedef struct
    {
    int x1;
    int y1;
    int r;
    } CIRCLE;


    int sl_getlength(vo id *ptr)
    {
    STRAIGHTLINE *sl = ptr;
    return (int) sqrt( (sl->x1 - sl->x2)*(sl->x1-sl->x2) +( sl->y1-sl->y2) *
    (sl->y1-sl->y2));
    }

    int circ_getlength( void *ptr)
    {
    CIRCLE *c = ptr;
    return (int) (3.14 * c->r * c->r);
    }

    Now we give a little alien a path.

    First set up a LINE with function pointers. Then call a dynamic function.

    void alien_attack(LI NE *path, void *ptr)
    {
    int Nsteps;
    int i;

    Nsteps = path->getlength(ptr) ;
    for(i=0;i<Nstep s;i++)
    {
    /* display alien following attackpath */
    }
    }


    --
    Free games and programming goodies.


    Comment

    • Ben Bacarisse

      #3
      Re: dynamic binding

      aarklon@gmail.c om writes:
      Hi all,
      >
      recently a friend asked me is there any dynamic binding in C...??
      to which i answered AFAIK it is in C++ only,
      but he says it is valid in C.
      This is a terminology nightmare since the phrase means different things
      to different people. If we copy the common C++ meaning (essentially
      virtual functions[1]) we can do it in C. The more flexible meaning (say
      the one used in CLOS) requires non-standard features.
      if dynamic can be implemented via function pointers in C ,
      can anyone give an example for dynamic binding in C...??
      The usual way to set it up is with a table of function pointers
      embedded in a structure. A "derived" type optionally adds data to the
      structure and the "constructo r" stores new function pointers either in
      the old table or in a new one. If the old table is re-used some trick
      is needed to save the old function pointer so it can still be used.

      There is a lot of "boilerplat e" to do this in C and I am reluctant to
      type it all out. Maybe someone has a real example in use?

      [1] I have seen this described as "static dynamic binding"!

      --
      Ben.

      Comment

      • cr88192

        #4
        Re: dynamic binding


        "Ben Bacarisse" <ben.usenet@bsb .me.ukwrote in message
        news:87ir0lrvpt .fsf@bsb.me.uk. ..
        aarklon@gmail.c om writes:
        >
        >Hi all,
        >>
        >recently a friend asked me is there any dynamic binding in C...??
        >to which i answered AFAIK it is in C++ only,
        > but he says it is valid in C.
        >
        This is a terminology nightmare since the phrase means different things
        to different people. If we copy the common C++ meaning (essentially
        virtual functions[1]) we can do it in C. The more flexible meaning (say
        the one used in CLOS) requires non-standard features.
        >
        or we could also say it is not possible...
        or that it is...

        the amazing power of terms...

        >if dynamic can be implemented via function pointers in C ,
        >can anyone give an example for dynamic binding in C...??
        >
        The usual way to set it up is with a table of function pointers
        embedded in a structure. A "derived" type optionally adds data to the
        structure and the "constructo r" stores new function pointers either in
        the old table or in a new one. If the old table is re-used some trick
        is needed to save the old function pointer so it can still be used.
        >
        There is a lot of "boilerplat e" to do this in C and I am reluctant to
        type it all out. Maybe someone has a real example in use?
        >
        what about a variation of delegation instead?...
        you create a new struct with a pointer to an instance of the old struct.
        calls into the new struct are handled as such, failing this, they are sent
        to the old struct.

        of course, usually I wrap my methods with ordinary functions, making it a
        little easier to hide the internal machinery...

        [1] I have seen this described as "static dynamic binding"!
        >
        --
        Ben.

        Comment

        • user923005

          #5
          Re: dynamic binding

          On Feb 19, 12:39 am, aark...@gmail.c om wrote:
          Hi all,
          >
          recently a friend asked me is there any dynamic binding in C...??
          to which i answered AFAIK it is in C++ only,
           but he says it is valid in C.
          >
          if dynamic can be implemented via function pointers in C ,
          can anyone give an example for dynamic binding in C...??
          It's not the same as C++, but you can do the rough equivalent.

          See:

          "Evaluation Machines" Link:


          Ernst Heinz uses replaceable chess evaluation functions, which depend
          upon the phase of the game (For instance, in the opening, the king
          stays on his home row and tries to hide in a corner under some pawns.
          But in the endgame, the king runs to the center of the board.)

          See:

          This is a post that will grow in the next few days. So please come back regularly and comment, because feedback helps me to think. Update 1 ...

          Comment

          Working...