DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Help on function pointers (http://www.daniweb.com/forums/thread105805.html)

Conqueror_2 Jan 22nd, 2008 7:24 pm
Help on function pointers
 
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.

//Main
int main()
{
 teste teste_classe;

// this works fine 
 cout << teste_classe.genericfunc(1.0,2.0,sum_2)<< endl;

// this returns an error saying function is not of type double(*)(double,double) !!!
 cout << teste_classe.genericfunc(1.0,2.0, teste_classe.sum  )<< endl;
 
 system("pause");
 return 0;
};

//Header

double sum_2(double a, double b);

class teste
{
 public:

 double sum(double x, double y);
 
 double genericfunc(double x, double y, double (*f)(double x, double y) );     
};

//Cpp

#include "teste.hpp"

double sum_2(double a, double b)
{
 return a+b;
};

double teste::sum(double x, double y)
{
 return x+y;
};     

double teste::genericfunc(double x, double y, double (*f)(double x, double y))
{
 return f(x,y);
};

Duoas Jan 22nd, 2008 7:34 pm
Re: Help on function pointers
 
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.

Conqueror_2 Jan 22nd, 2008 7:48 pm
Re: Help on function pointers
 
So functors are the way to go? Never would have guessed!
Thanks for your reply and the reference to the tutorial.

Duoas Jan 22nd, 2008 8:36 pm
Re: Help on function pointers
 
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:

#include <iostream>
using namespace std;

//----------------------------------------
// This is our functor class.
//----------------------------------------
// Currently it is rather simplistic as the point of a functor is
// to hold state, and this class is stateless (it has no member
// variables). In essence, it is just a callable object.
//
class ftor {
  public:
    int operator () ( int augend, int addend ) {
      return augend + addend;
      }
  };

//----------------------------------------
// Here is a normal, everyday function.
//----------------------------------------
int func( int augend, int addend ) {
  return augend + addend;
  }

//----------------------------------------
// Here is a generic function.
//----------------------------------------
// That's "generic" in the C++ sense of the word.
// The function is used below with two different
// types of arguments, so the compiler actually
// creates two functions.
//
template <typename predicate>
void do_something( predicate f, std::string info ) {
  cout << "10 + 5 = "
      << f( 10, 5 )
      << " (" << info << ')'
      << endl;
  }

//----------------------------------------
// Examples of using the generic function
//----------------------------------------
int main() {
  do_something( func,  "using a regular function" );
  do_something( ftor(), "using a temporary functor" );
  ftor inst;
  do_something( inst,  "using a locally instantiated functor" );
  }

Hope this makes things a little clearer with functors. Alas, I think I didn't really answer your original problem.


All times are GMT -4. The time now is 12:30 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC