473,398 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Constructors/Destructors for struct with function pointers

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

Nov 14 '05 #1
3 2935
Takeshi <do************@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 ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2
Je***********@physik.fu-berlin.de wrote:
Takeshi <do************@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")


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 (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #3
Many thanks - much appreciated

CBFalconer wrote:
Je***********@physik.fu-berlin.de wrote:
Takeshi <do************@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")

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.


Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have...
7
by: joevandyk | last post by:
Below, I have a class Container that contains a vector. The vector contains pointers to a Base class. When the Container destructor is called, I would like to delete all the objects that the...
3
by: rahul8143 | last post by:
hello, I write a following program and have problem in understanding constructors and destructors. #include <iostream.h> class vector { public: double x; double y;
9
by: Peter Oliphant | last post by:
I've been told that value structs can have default constructors (I'm using VS C++.NET 2005 PRO using clr:/pure syntax). But the following code generates the following error: value struct...
5
by: Alok | last post by:
hii Would somebody clear my doubts on following qustions . What are virtual constructors and virtual destructors ? Why can't we have virtual constructors ? What are demerits of inheritence in...
4
by: JoeC | last post by:
I am trying to design some complex objects that have quite a bit of data. I understand most syntax but I am trying to learn how to make better design choices. The first question is to OK or good...
21
by: Michael Hull | last post by:
Hi, I remember from somewhere reading that inlining constructors is a 'BadThing', but now can't seem to find the original source. I can't however thing of a reason why it would be for simple...
8
by: Shraddha | last post by:
What is the use of "PURE vitual distructors"? And why we can not have vitual constructors?
2
by: Eric Lilja | last post by:
As the topic says, I wanted to make a re-usable singleton class that could create pointers to objects with non-trivial constructors. I came up with this: #ifndef SINGLETON_HPP #define...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.