By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,986 Members | 1,944 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,986 IT Pros & Developers. It's quick & easy.

In pointers to functions in C++

P: 62
Hi all,

In pointers to functions in C++

The below declaration

typedef int (*CFT )(const void *, const void *);

what does this declaration mean?

Is it that inorder to use the pointer to function just by using CFT can we use

the pointer to that function.

give me an example where we can use CFT.

regards

srikar
Sep 21 '06 #1
Share this Question
Share on Google+
7 Replies


Banfa
Expert Mod 5K+
P: 8,916
It starts with typedef. That means you are defining a data type. This will not result in any usable data but a type with which you can declare data.

Here is an example usage

Expand|Select|Wrap|Line Numbers
  1. typedef  int (*CFT )(const void *, const void *);
  2.  
  3. int CompareLocations(const void *p1; const void *p2)
  4. {
  5.     if (p1>p2)
  6.         return 1;
  7.     else if (p1<p2)
  8.         return -1;
  9.     else return 0;
  10. }
  11.  
  12. int PointerSort(const void**pp1, CFT compareFunction)
  13. {
  14.     // some code here caling
  15.  
  16.     int result = compareFunction(pp1[x], pp1[y]);
  17. }
  18.  
  19. int main(int argc, char **argp)
  20. {
  21.     void *ptrArray[100];
  22.  
  23.     // code to fill in ptrArray
  24.  
  25.     PointerSort(ptrArray, CompareLocations);
  26. }
  27.  
P.S. I have no idea where you might use this rather contrieved example but passing pointers to callback functions is not entirely uncommon in C
Sep 21 '06 #2

P: 62
It starts with typedef. That means you are defining a data type. This will not result in any usable data but a type with which you can declare data.

Here is an example usage

Expand|Select|Wrap|Line Numbers
  1. typedef  int (*CFT )(const void *, const void *);
  2.  
  3. int CompareLocations(const void *p1; const void *p2)
  4. {
  5.     if (p1>p2)
  6.         return 1;
  7.     else if (p1<p2)
  8.         return -1;
  9.     else return 0;
  10. }
  11.  
  12. int PointerSort(const void**pp1, CFT compareFunction)
  13. {
  14.     // some code here caling
  15.  
  16.     int result = compareFunction(pp1[x], pp1[y]);
  17. }
  18.  
  19. int main(int argc, char **argp)
  20. {
  21.     void *ptrArray[100];
  22.  
  23.     // code to fill in ptrArray
  24.  
  25.     PointerSort(ptrArray, CompareLocations);
  26. }
  27.  
P.S. I have no idea where you might use this rather contrieved example but passing pointers to callback functions is not entirely uncommon in C

Thanks Banfa for the reply,
Still i am not completely clear with this concept.
i am having some questions in your program

1. Here u have used the declaration CFT compareFunction is it that
this represents that compareFunction is a function of return type as int &
the parameters for this function are (const void*,const void*)

2. Is its that typedef is used for the creation of new data type or it is used
instead of existing particular data type
3. In the above program is it that CFT is the new data type that can be used for
functions which returns int & also with that void* parameters.

regards

srikar
Sep 21 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
1. Here u have used the declaration CFT compareFunction is it that this represents that compareFunction is a function of return type as int &
the parameters for this function are (const void*,const void*)

2. Is its that typedef is used for the creation of new data type or it is used
instead of existing particular data type

3. In the above program is it that CFT is the new data type that can be used for
functions which returns int & also with that void* parameters.
1. CFT compareFunction; declares the variable compareFunction as a pointer to a function that returns in and has parameters (const void*,const void*).

NOTE compareFunction is not a function it is a pointer (to a function).

2. typedefs are used to create new data types. However you will find many projects that declre typedefs like

typedef unsigned int UINT;

and then use UINT throughout the project rather than unsigned int.

3. Basically yes. CFT is a new data type of type pointer to function returning int with parameters (const void*,const void*), it is used to store a pointer to a function with that prototype.
Sep 21 '06 #4

P: 62
Thanks Banfa

So Inorder to invoke the call to the function , by assigning the address of function to the variable comparefunction and using the notation

comparefunction=&fun1
// assuming fun1 returns int and parametes as void*, void*

CFT comparefunction
//the above declaration will invoke the function.
is it correct banfa?

Thanks much Banfa
Sep 21 '06 #5

Banfa
Expert Mod 5K+
P: 8,916
It goes like this

Expand|Select|Wrap|Line Numbers
  1. // Declaration of a function
  2. int ActualFunction(const void *p1, const void *p2)
  3. {
  4.   // Code here
  5. }
  6.  
  7. // Declaration of a variable that is a pointer to a function
  8. CFT FunctionPointer;
  9.  
  10. // Assignment of the pointer to the function to the variable
  11. FunctionPointer = ActualFunction;
  12.  
  13. //Envoke the function through the pointer
  14. void *p1, p2;
  15. int i;
  16.  
  17. i = FunctionPointer(p1, p2);
  18.  
  19. //Alternate notation for envoking a function through a pointer
  20. i = (*FunctionPointer)(p1, p2);
  21.  
Sep 21 '06 #6

P: 62
It goes like this

Expand|Select|Wrap|Line Numbers
  1. // Declaration of a function
  2. int ActualFunction(const void *p1, const void *p2)
  3. {
  4.   // Code here
  5. }
  6.  
  7. // Declaration of a variable that is a pointer to a function
  8. CFT FunctionPointer;
  9.  
  10. // Assignment of the pointer to the function to the variable
  11. FunctionPointer = ActualFunction;
  12.  
  13. //Envoke the function through the pointer
  14. void *p1, p2;
  15. int i;
  16.  
  17. i = FunctionPointer(p1, p2);
  18.  
  19. //Alternate notation for envoking a function through a pointer
  20. i = (*FunctionPointer)(p1, p2);
  21.  

Thank you Banfa My doubt got cleared.
Sep 22 '06 #7

100+
P: 180
Hi,
Here is a sample program to illustrate pointer to function concept

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. bool less_than(int x, int y)
  5. {
  6.    return x<y?true:false;
  7. }
  8.  
  9. bool greater_than(int x, int y)
  10. {
  11.    return x>y?true:false;
  12. }
  13.  
  14. vector<int> filter(const vector<int>& vect, int filter_value, bool (*compare)(int, int))
  15. {
  16.   vector<int> v;
  17.   for(int i=0; i<vect.size(); ++i)
  18.     if(compare(vect[i], filter_value))
  19.        v.push_back(vect[i]);
  20.  
  21.   return v;
  22.  
  23. int main( int argc, char** argv[])
  24. {
  25.    int arr[] = {2,56,3,2,6,78,34,7,89,0,22};
  26.    int arr_length = sizeof(arr)/ sizeof(arr[0]);
  27.    vector<int>vec(arr, arr+arr_length);
  28.    vector<int> v = filter(vec, 10, less_than);
  29.    //or use vector<int> v = filter(vec, 10, greater_than);
  30.  
  31.    for(int i=0; i<v.size(); ++i)
  32.     cout<<v[i]<<"\t";
  33.  
  34.    return 0;
  35. }
Sep 23 '06 #8

Post your reply

Sign in to post your reply or Sign up for a free account.