Constructors/Destructors for struct with function pointers

T

Takeshi

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 */
}


Many thanks in advance
 
J

Jens.Toerring

Takeshi said:
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
 
C

CBFalconer

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.
 
T

Takeshi

Many thanks - much appreciated
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top