cfaun5
06-16-2003, 12:06 PM
What does the following mean?:
typedef int (*BINDFN) (EObjectType cdktype, void *object, void *clientData, chty
pe input);
typedef int (*BINDFN) (EObjectType cdktype, void *object, void *clientData, chty
pe input);
|
Click to See Complete Forum and Search --> : C question: weird statement in header file cfaun5 06-16-2003, 12:06 PM What does the following mean?: typedef int (*BINDFN) (EObjectType cdktype, void *object, void *clientData, chty pe input); evac-q8r 06-16-2003, 12:26 PM Hi cfaun5, Statements such as these become pretty routine after alot of exposure. Anyways it is defining a TYPE which is called BINDFN. So you would expect to see BINDFN in a variable decleration statement as the type followed by a variable for example... func(){ BINDFN your_function; .... } your_function is now a variable name for a pointer to a function which takes EObjectType cdktype, void *object, void *clientData, chtype input as parameters to the function. Upon receiving these these parameters when the function is called ( a function may be called as an ordinary function or through a pointer to the address of the function ) it will return an integer type. Make any sense. Just keep in mind that it defines a TYPE and thats it. You must then define or declare variables which will be of this TYPE --> BINDFN as I have shown above. EVAC cfaun5 06-16-2003, 02:03 PM Perfect sense. Thank you!!!!!!!!!!!!!!!!!!!!!!!! nowonmai 06-17-2003, 05:01 AM why delete the original question? now other people who may be having the same problem won't be able to get an answer and will post the same thing again... :rolleyes: cfaun5 06-17-2003, 11:57 AM Sorry. I deleted it before I got the response. Evac-q8r must have posted while I was deleting it or have admin privalleges and looked at the history. Sorry again. I reconstructed the best I could. nowonmai 06-18-2003, 05:19 AM thanks mate... good question too... Sepero 06-19-2003, 06:17 AM I'm Java programmer trying to learn C++ and this seems strange to me. "your_function is now a variable name for a pointer to a function..." You can have pointers to variables AND functions? chrism01 06-19-2003, 06:47 AM Yes. Personally I dislike ptrs to fns (I use straight C, not C++), but its perfectly legal. I believe a lot of MSWin is written using this method. I've found that in a debugger, if such a thing gets called, you get the mem address of the fn instead of the fn name, which is a pain if someone is indexing into an array of these. You can't tell which one is called (easily). Just my opinion evac-q8r 06-19-2003, 08:06 AM Yep, I think they are really cool. The only thing I dislike about them is that if you have many different function types, for example, a function which receives nothing and returns nothing, or a function which receives an integer and returns an integer. If you use a function pointer, that is, you define a variable you must assign it to an existing function with the identical type in which you declared the pointer to be. This can be difficult to accomplish on a large scale. The really cool thing about these function pointers is that you can declare them within structure variables. So you can pass a structure to a function and operate on the members in that structure and then because you have a pointer to a function in that structure you may pass those modified values to the function pointer and do some other calculation. Sort of like C++ because now you may limit yourself to operate on particular variables (structures) only with functions which reside inside of that structure. EVAC Stuka 06-19-2003, 09:23 AM Yup, function pointers are very useful for certain things in C which would otherwise be difficult. Callbacks in GTK+ (and any other C based GUI that I can think of) are function pointers, and the PCI system in Linux uses them for handling different lower-level issues from what a friend told me (and he'd know, I assure you ;) ). They can be a pain, but they are invaluable for certain types of coding. RedMap 06-19-2003, 01:04 PM Yes, very useful but it is often easier to make mistakes using them, if a lot of MSWin is written using this method then I can understand why their software is so buggy. justlinux.com
Copyright Internet.com Inc. All Rights Reserved. |