Help on function pointers

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2008
Posts: 2
Reputation: Conqueror_2 is an unknown quantity at this point 
Solved Threads: 0
Conqueror_2 Conqueror_2 is offline Offline
Newbie Poster

Help on function pointers

 
0
  #1
Jan 22nd, 2008
I get an error on the following code. It seems I can“t use a function which belongs to a class as an argument for "genericfunction".
Any help on how to overcome the problem would be great.
Thanks.

  1. //Main
  2. int main()
  3. {
  4. teste teste_classe;
  5.  
  6. // this works fine
  7. cout << teste_classe.genericfunc(1.0,2.0,sum_2)<< endl;
  8.  
  9. // this returns an error saying function is not of type double(*)(double,double) !!!
  10. cout << teste_classe.genericfunc(1.0,2.0, teste_classe.sum )<< endl;
  11.  
  12. system("pause");
  13. return 0;
  14. };
  15.  
  16. //Header
  17.  
  18. double sum_2(double a, double b);
  19.  
  20. class teste
  21. {
  22. public:
  23.  
  24. double sum(double x, double y);
  25.  
  26. double genericfunc(double x, double y, double (*f)(double x, double y) );
  27. };
  28.  
  29. //Cpp
  30.  
  31. #include "teste.hpp"
  32.  
  33. double sum_2(double a, double b)
  34. {
  35. return a+b;
  36. };
  37.  
  38. double teste::sum(double x, double y)
  39. {
  40. return x+y;
  41. };
  42.  
  43. double teste::genericfunc(double x, double y, double (*f)(double x, double y))
  44. {
  45. return f(x,y);
  46. };
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Help on function pointers

 
0
  #2
Jan 22nd, 2008
No, you can't, because member functions take an extra, hidden, argument this.

You might want to check out functors, which would solve your problem! (While you are at it, the entire Function Pointer Tutorials site is worth a good perusal.)

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2
Reputation: Conqueror_2 is an unknown quantity at this point 
Solved Threads: 0
Conqueror_2 Conqueror_2 is offline Offline
Newbie Poster

Re: Help on function pointers

 
0
  #3
Jan 22nd, 2008
So functors are the way to go? Never would have guessed!
Thanks for your reply and the reference to the tutorial.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Help on function pointers

 
0
  #4
Jan 22nd, 2008
Hmm, as I re-read your code your "generic function" seems to me to be very un-generic --at least in the C++ sense of the word. Hence my suggestion for functors.

If you really want to use function pointers directly you will have to figure some other way to fix it. Otherwise you will need to implement it in terms of C++ generics. Here's an example:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. //----------------------------------------
  5. // This is our functor class.
  6. //----------------------------------------
  7. // Currently it is rather simplistic as the point of a functor is
  8. // to hold state, and this class is stateless (it has no member
  9. // variables). In essence, it is just a callable object.
  10. //
  11. class ftor {
  12. public:
  13. int operator () ( int augend, int addend ) {
  14. return augend + addend;
  15. }
  16. };
  17.  
  18. //----------------------------------------
  19. // Here is a normal, everyday function.
  20. //----------------------------------------
  21. int func( int augend, int addend ) {
  22. return augend + addend;
  23. }
  24.  
  25. //----------------------------------------
  26. // Here is a generic function.
  27. //----------------------------------------
  28. // That's "generic" in the C++ sense of the word.
  29. // The function is used below with two different
  30. // types of arguments, so the compiler actually
  31. // creates two functions.
  32. //
  33. template <typename predicate>
  34. void do_something( predicate f, std::string info ) {
  35. cout << "10 + 5 = "
  36. << f( 10, 5 )
  37. << " (" << info << ')'
  38. << endl;
  39. }
  40.  
  41. //----------------------------------------
  42. // Examples of using the generic function
  43. //----------------------------------------
  44. int main() {
  45. do_something( func, "using a regular function" );
  46. do_something( ftor(), "using a temporary functor" );
  47. ftor inst;
  48. do_something( inst, "using a locally instantiated functor" );
  49. }

Hope this makes things a little clearer with functors. Alas, I think I didn't really answer your original problem.
Last edited by Duoas; Jan 22nd, 2008 at 8:37 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC