Constructors/Destructors for struct with function pointers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Takeshi

    #1

    Constructors/Destructors for struct with function pointers

    I have code as ff:

    typedef double* (*DBLPTRFUNCPTR )(int) ;
    typedef int* (*INTPTRFUNCPTR )(int) ;
    typedef double (*DBLFUNCPTR)(i nt) ;
    typedef int (*INTFUNCPTR)(i nt) ;
    etc ....


    typedef struct
    {
    double *data;
    int size;
    int numcols;
    int currcol;
    DBLPTRFUNCPTR New ;
    VOIDFUNCPTR Destroy ;
    DBLFUNCPTR GetItem ;
    VOIDFUNCPTR2 SetItem ;
    } FArray, *FArrayPtr;


    How can I pass a "this" ptr (i.e. ptr to the struct to my
    allocate/dealllocate functions so I can write code like this (Yes I know
    it is a "no-brainer in C++, but I have to implement this in Ansi C -
    Basically, I'm porting C++ code using STL vectors, and I need this
    functionality as a "wrapper")

    /* Sample code*/

    void foo( void ) {
    double tmp ;
    FArray array ;

    array.New(10) /* Allocate a 1D array with 10 rows */
    array.SetItem(1 )= 3.142 ;
    tmp = array.GetItem(1 ) ;

    array.Destroy() /* Free memory */
    }


    Many thanks in advance

  • Jens.Toerring@physik.fu-berlin.de

    #2
    Re: Constructors/Destructors for struct with function pointers

    Takeshi <do.not.spam.me @work.com> wrote:[color=blue]
    > I have code as ff:[/color]
    [color=blue]
    > typedef double* (*DBLPTRFUNCPTR )(int) ;
    > typedef int* (*INTPTRFUNCPTR )(int) ;
    > typedef double (*DBLFUNCPTR)(i nt) ;
    > typedef int (*INTFUNCPTR)(i nt) ;
    > etc ....[/color]

    [color=blue]
    > typedef struct
    > {
    > double *data;
    > int size;
    > int numcols;
    > int currcol;
    > DBLPTRFUNCPTR New ;
    > VOIDFUNCPTR Destroy ;
    > DBLFUNCPTR GetItem ;
    > VOIDFUNCPTR2 SetItem ;
    > } FArray, *FArrayPtr;[/color]
    [color=blue]
    > How can I pass a "this" ptr (i.e. ptr to the struct to my
    > allocate/dealllocate functions so I can write code like this (Yes I know
    > it is a "no-brainer in C++, but I have to implement this in Ansi C -
    > Basically, I'm porting C++ code using STL vectors, and I need this
    > functionality as a "wrapper")[/color]
    [color=blue]
    > /* Sample code*/[/color]
    [color=blue]
    > void foo( void ) {
    > double tmp ;
    > FArray array ;[/color]
    [color=blue]
    > array.New(10) /* Allocate a 1D array with 10 rows */
    > array.SetItem(1 )= 3.142 ;
    > tmp = array.GetItem(1 ) ;[/color]
    [color=blue]
    > array.Destroy() /* Free memory */
    > }[/color]

    You must pass the address of the structure as another function argument
    to your "class" functions yourself. I.e. make e.g
    [color=blue]
    > typedef double* ( *DBLPTRFUNCPTR )( FArrayPtr, int ) ;[/color]

    and call it as

    array.New( &array, 10 );

    etc. There's no automatic passing of the structure to the array as
    you might be used to from objects in C++.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ Jens.Toerring@p hysik.fu-berlin.de
    \______________ ____________ http://www.toerring.de

    Comment

    • CBFalconer

      #3
      Re: Constructors/Destructors for struct with function pointers

      Jens.Toerring@p hysik.fu-berlin.de wrote:[color=blue]
      > Takeshi <do.not.spam.me @work.com> wrote:
      >[color=green]
      >> I have code as ff:[/color]
      >[color=green]
      >> typedef double* (*DBLPTRFUNCPTR )(int) ;
      >> typedef int* (*INTPTRFUNCPTR )(int) ;
      >> typedef double (*DBLFUNCPTR)(i nt) ;
      >> typedef int (*INTFUNCPTR)(i nt) ;
      >> etc ....[/color]
      >[color=green]
      >> typedef struct
      >> {
      >> double *data;
      >> int size;
      >> int numcols;
      >> int currcol;
      >> DBLPTRFUNCPTR New ;
      >> VOIDFUNCPTR Destroy ;
      >> DBLFUNCPTR GetItem ;
      >> VOIDFUNCPTR2 SetItem ;
      >> } FArray, *FArrayPtr;[/color]
      >[color=green]
      >> How can I pass a "this" ptr (i.e. ptr to the struct to my
      >> allocate/dealllocate functions so I can write code like this (Yes
      >> I know it is a "no-brainer in C++, but I have to implement this
      >> in Ansi C - Basically, I'm porting C++ code using STL vectors,
      >> and I need this functionality as a "wrapper")[/color][/color]

      Piggybacking on Jens posting, since the original has not appeared
      here.

      You might look at my recently released:

      <http://cbfalconer.home .att.net/download/id2id-20.zip>

      for a somewhat similar application. It is all in the interface to
      the hashlib library, of which a complete but minimum instance is
      included. If you back down to the download directory you will also
      find the hashlib.zip, and other things.

      I suggest you think in C, not in C++ for the coding. In the above
      cases the hashtable is the object, into which other things can be
      inserted, found, deleted, serialized, etc. In a wide variety of
      cases those other objects are strings and auxiliary data, but they
      can be anything.

      --
      Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
      Available for consulting/temporary embedded and systems.
      <http://cbfalconer.home .att.net> USE worldnet address!


      Comment

      • Takeshi

        #4
        Re: Constructors/Destructors for struct with function pointers

        Many thanks - much appreciated

        CBFalconer wrote:[color=blue]
        > Jens.Toerring@p hysik.fu-berlin.de wrote:
        >[color=green]
        >>Takeshi <do.not.spam.me @work.com> wrote:
        >>
        >>[color=darkred]
        >>>I have code as ff:[/color]
        >>[color=darkred]
        >>>typedef double* (*DBLPTRFUNCPTR )(int) ;
        >>>typedef int* (*INTPTRFUNCPTR )(int) ;
        >>>typedef double (*DBLFUNCPTR)(i nt) ;
        >>>typedef int (*INTFUNCPTR)(i nt) ;
        >>>etc ....[/color]
        >>[color=darkred]
        >>>typedef struct
        >>>{
        >>> double *data;
        >>> int size;
        >>> int numcols;
        >>> int currcol;
        >>> DBLPTRFUNCPTR New ;
        >>> VOIDFUNCPTR Destroy ;
        >>> DBLFUNCPTR GetItem ;
        >>> VOIDFUNCPTR2 SetItem ;
        >>>} FArray, *FArrayPtr;[/color]
        >>[color=darkred]
        >>>How can I pass a "this" ptr (i.e. ptr to the struct to my
        >>>allocate/dealllocate functions so I can write code like this (Yes
        >>>I know it is a "no-brainer in C++, but I have to implement this
        >>>in Ansi C - Basically, I'm porting C++ code using STL vectors,
        >>>and I need this functionality as a "wrapper")[/color][/color]
        >
        >
        > Piggybacking on Jens posting, since the original has not appeared
        > here.
        >
        > You might look at my recently released:
        >
        > <http://cbfalconer.home .att.net/download/id2id-20.zip>
        >
        > for a somewhat similar application. It is all in the interface to
        > the hashlib library, of which a complete but minimum instance is
        > included. If you back down to the download directory you will also
        > find the hashlib.zip, and other things.
        >
        > I suggest you think in C, not in C++ for the coding. In the above
        > cases the hashtable is the object, into which other things can be
        > inserted, found, deleted, serialized, etc. In a wide variety of
        > cases those other objects are strings and auxiliary data, but they
        > can be anything.
        >[/color]

        Comment

        Working...