Hello world,
many readers helped me with my struct query,
so I fell encouraged to post another question.
I wonder if linking functions via pointers
to structs is "a good idea". E.g. a stack
struct stack
{
/* some var in here */
void (*push)(<pointer to stack>, <object>);
};
/* ... */
struct stack test;
/* ... */
test.push(&test, <stuff>);
Since there is no "this" pointer in C (at least I
do not know), is there an advantage in using an object-
like structure (not only member-var but also functions
packed together)?
Well, or does it depend on the situation?
Thanks
Michael 10 2550
On Tue, 27 Jan 2004 09:57:03 +0100, Michael wrote: Hello world, many readers helped me with my struct query, so I fell encouraged to post another question. I wonder if linking functions via pointers to structs is "a good idea". E.g. a stack
struct stack { /* some var in here */ void (*push)(<pointer to stack>, <object>); }; /* ... */ struct stack test;
push = pointer_to_push_function;
And you have to remember to to that each and every time you make a
stack, for every function. Kinds of kills the benefit.
/* ... */ test.push(&test, <stuff>);
Since there is no "this" pointer in C (at least I do not know), is there an advantage in using an object- like structure (not only member-var but also functions packed together)? Well, or does it depend on the situation?
Yes there are advantages. C++ was created for when you need those and are
willing to pay the price. If you're a little structured (no pun intended)
you can do without having the function as part of the struct (unless you
are going to do something strange like change the push function at
runtime) and save having all those pointers in your structs.
#include <stdio.h>
struct stack
{
int *blah;
};
int stack_push(struct stack*,int);
int stack_pop(struct stack*,int*);
int stack_count(struct stack*,int*);
/* implementation here */
int main(void) {
struct stack test;
int value;
if (stack_push(&test,42)) {
/* successfully pushed onto stack */
} else {
/* error handling */
}
/* do stufF */
if (stack_pop(&test,&value)) {
/* successfully popped stack */
} else {
/* failed, perhaps the stack was empty */
/* error handling */
}
return 0;
}
--
NPV
"the large print giveth, and the small print taketh away"
Tom Waits - Step right up
On Tue, 27 Jan 2004 09:57:03 +0100, Michael <mi****@gmx.net> wrote: Hello world, many readers helped me with my struct query, so I fell encouraged to post another question. I wonder if linking functions via pointers to structs is "a good idea". E.g. a stack
It is not uncommon to use this way. struct stack { /* some var in here */ void (*push)(<pointer to stack>, <object>); }; /* ... */ struct stack test; /* ... */ test.push(&test, <stuff>);
Since there is no "this" pointer in C (at least I do not know), is there an advantage in using an object- like structure (not only member-var but also functions packed together)? Well, or does it depend on the situation?
thats how we implement virtual functions in C.
It is advantageous in some situations.
Thanks Michael
Michael wrote: Hello world, many readers helped me with my struct query, so I fell encouraged to post another question. I wonder if linking functions via pointers to structs is "a good idea". E.g. a stack
struct stack { /* some var in here */ void (*push)(<pointer to stack>, <object>); }; /* ... */ struct stack test; /* ... */ test.push(&test, <stuff>);
Since there is no "this" pointer in C (at least I do not know), is there an advantage in using an object- like structure (not only member-var but also functions packed together)? Well, or does it depend on the situation?
I use arrays of structures with function pointers,
to race sorting functions against each other,
with various array distributions.
struct sf {
void(*sortfunc)(e_type *, size_t);
char *name;
char *description;
double total_time;
};
struct sf s_L[] = SORT_LIST;
struct df {
void(*distfunc)(e_type *, size_t);
char *string;
} d_L[] = DISTRIBUTION_LIST;
--
pete
Michael wrote: Hello world, many readers helped me with my struct query, so I fell encouraged to post another question. I wonder if linking functions via pointers to structs is "a good idea". E.g. a stack
struct stack { /* some var in here */ void (*push)(<pointer to stack>, <object>); }; /* ... */ struct stack test; /* ... */ test.push(&test, <stuff>);
Since there is no "this" pointer in C (at least I do not know), is there an advantage in using an object- like structure (not only member-var but also functions packed together)? Well, or does it depend on the situation? Thanks Michael
This is essentially how Motif works, with one major exception: you
define two structs; one has only one (global) instance, and the other
has a pointer to that global instance.
For example (much simplified):
/* First, declare function typoes */
typedef void (*MyProcA)( int n, char *s );
typedef int (*MyProcB)( double d );
/* Provide prototypes for your functions */
void ProcA( int n, char *s);
int ProcB( double d);
/* Define the class struct, and an instance of it */
typedef struct _ClassRec {
MyProcA ProcA;
MyProcB ProcB;
}ClassRec;
ClassRec classRec;
/* Define instance struct */
typedef struct _InstanceRec {
ClassRec *class_rec_ptr;
/* plus any instance variables */
...
}InstanceRec;
Then define your code - instances of InstanceRec, functions ProcA and
ProcB, etc.
This creates a pseudo-object-oriented paradigm from the
non-object-oriented C language, in that when you create instances of
struct InstanceRec, your code can access the functions defined in
ClassRec.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225
Nils Petter Vaskinn wrote: On Tue, 27 Jan 2004 09:57:03 +0100, Michael wrote:
Hello world, many readers helped me with my struct query, so I fell encouraged to post another question. I wonder if linking functions via pointers to structs is "a good idea". E.g. a stack
struct stack { /* some var in here */ void (*push)(<pointer to stack>, <object>); }; /* ... */ struct stack test;
push = pointer_to_push_function;
And you have to remember to to that each and every time you make a stack, for every function. Kinds of kills the benefit.
Well, as anyone writing such code will also create an init() function,
this is not really a problem. /* ... */ test.push(&test, <stuff>);
Since there is no "this" pointer in C (at least I do not know), is there an advantage in using an object- like structure (not only member-var but also functions packed together)? Well, or does it depend on the situation?
Yes there are advantages. C++ was created for when you need those and are willing to pay the price. If you're a little structured (no pun intended) you can do without having the function as part of the struct (unless you are going to do something strange like change the push function at runtime) and save having all those pointers in your structs.
Right, but the function-ptr-in-the-struct option let you have some kind
of polymorphism, that you can't have (in C) with the classical-ADT option.
Now it's also clear that when you come to this, it's time to consider
using another language (if possible !-)
Bruno
> Since there is no "this" pointer in C (at least I do not know), is there an advantage in using an object- like structure (not only member-var but also functions packed together)? Well, or does it depend on the situation?
You could do something like this:
/* Stack interface */
typedef struct IStack_
{
void ( *fpPush ) ( struct IStack_ *pStack, const void *pUserState );
void* ( *fpPop ) ( struct IStack_ *pStack );
void* ( *fpFree ) ( struct IStack_ *pStack );
void *pImpl;
} *IStack;
/* Stack API */
#define StackPush( i, us ) ( (i)->fpPush( (i), (us) ) )
#define StackPop( i ) ( (i)->fpPop( (i) ) )
#define StackFree( i ) ( (i)->fpFree( (i) ) )
Then you could easily create different types of stacks that share a single
interface.
OOP in C can be easy and flexible...
Michael <mi****@gmx.net> wrote in message news:<bv**********@rzcomm2.rz.tu-bs.de>... Hello world, many readers helped me with my struct query, so I fell encouraged to post another question. I wonder if linking functions via pointers to structs is "a good idea". E.g. a stack
struct stack { /* some var in here */ void (*push)(<pointer to stack>, <object>); }; /* ... */ struct stack test; /* ... */ test.push(&test, <stuff>);
Since there is no "this" pointer in C (at least I do not know), is there an advantage in using an object- like structure (not only member-var but also functions packed together)? Well, or does it depend on the situation? Thanks Michael
You can do this, and as other posters have pointed out there are other
ways to do it that can make things a little easier.
If you can avoid this programming style, it often becomes confusing if
your not very careful.
Define the struct in a .h file, put the functions that act upon it in
the corresponding .c file.
Only think about putting function pointers in the struct if you might
change what they point to somewhere in the program. So in your
example it is not a good idea to do so with push and pop.
Hi,
thanks to all of you, I guess
I am going to avoid function-pointers.
Michael
> thanks to all of you, I guess I am going to avoid function-pointers.
I would not rule out using function-pointers. They are a great tool that
makes generic programming in C a piece of cake.
;)
On Wed, 28 Jan 2004 16:32:46 +0100, Michael wrote: Hi, thanks to all of you, I guess I am going to avoid function-pointers.
Don't. Just avoid them when you don't need them, or there are better ways
to achieve what you want. Function pointers can be poverful it just that
using them like that might mean you should reconsider either your design
or your choice of implementation language.
Now we never asked what the purpose of your program was. If it was simply
a toy, to learn about function pointers and experiment with object
orientation in C, you should go ahead and do it your way anyway.
If you wanted to do this to learn you could even do several
paralell versions, using different techniques.
--
NPV
"the large print giveth, and the small print taketh away"
Tom Waits - Step right up This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: dasod |
last post by:
I would like to know if my method to remove list objects is correct in
this small test program. It seems to me that there might be a simplier
way, but I'm afraid I don't know enough about list...
|
by: Aguilar, James |
last post by:
Hey guys. A new question:
I want to use an STL libarary to hold a bunch of objects I create.
Actually, it will hold references to the objects, but that's beside the
point, for the most part. ...
|
by: Alfonso Morra |
last post by:
I have written the following code, to test the concept of storing
objects in a vector. I encounter two run time errors:
1). myClass gets destructed when pushed onto the vector
2). Prog throws a...
|
by: ytrewq |
last post by:
Should dynamic ("expando") properties be restricted to native and
user-defined objects? Or should host objects - such as references to
the browser or a plug-in or to the document and its elements -...
|
by: Lüpher Cypher |
last post by:
Hi,
Suppose we have a hierarchical class structure that looks something like
this:
Object
|
+-- Main
|
+-- Object1
|
by: KraftDiner |
last post by:
I was under the assumption that everything in python was a refrence...
so if I code this:
lst =
for i in lst:
if i==2:
i = 4
print lst
I though the contents of lst would be modified.....
|
by: Jo |
last post by:
Hi,
How can i differentiate between static and dynamic allocated objects?
For example:
void SomeFunction1() {
CObject *objectp = new CObject;
CObject object;
|
by: George Exarchakos |
last post by:
Hi everyone,
I'd like your help...
Can we have a std::list<BASEwhere BASE be the base class of a class
hierarchy? I want to add to this list objects that are inherited from
BASE class but not...
|
by: SasQ |
last post by:
Hello.
I wonder if literal constants are objects, or they're only
"naked" values not contained in any object?
I have read that literal constants may not to be allocated
by the compiler. If the...
|
by: Jess |
last post by:
Hello,
I learned that there are five kinds of static objects, namely
1. global objects
2. object defined in namespace scope
3. object declared static instead classes
4. objects declared...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
| |