Re: Constructors/Destructors for struct with function pointers

Jens.Toerring_at_physik.fu-berlin.de
Date: 10/31/04


Date: 31 Oct 2004 19:13:50 GMT

Takeshi <do.not.spam.me@work.com> wrote:
> I have code as ff:

> typedef double* (*DBLPTRFUNCPTR)(int) ;
> typedef int* (*INTPTRFUNCPTR)(int) ;
> typedef double (*DBLFUNCPTR)(int) ;
> typedef int (*INTFUNCPTR)(int) ;
> 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 */
> }

You must pass the address of the structure as another function argument
to your "class" functions yourself. I.e. make e.g

> typedef double* ( *DBLPTRFUNCPTR )( FArrayPtr, int ) ;

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@physik.fu-berlin.de
   \__________________________  http://www.toerring.de