| | |
Help on function pointers
Thread Solved
![]() |
•
•
Join Date: Jan 2008
Posts: 2
Reputation:
Solved Threads: 0
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.
Any help on how to overcome the problem would be great.
Thanks.
C++ Syntax (Toggle Plain Text)
//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); };
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.
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.
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:
Hope this makes things a little clearer with functors. Alas, I think I didn't really answer your original problem.
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:
C++ Syntax (Toggle Plain Text)
#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.
Last edited by Duoas; Jan 22nd, 2008 at 8:37 pm.
![]() |
Similar Threads
- c language problm, how to pass pointer to a function (C)
- Information on function pointers (C)
- Stack implementation using function pointers in C (C)
- Function pointers inside classes (C++)
Other Threads in the C++ Forum
- Previous Thread: mouse functions for DOS
- Next Thread: How sort a Dynamic Array?
| Thread Tools | Search this Thread |
action api array auto based beginner binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count createcopyofanyfileinc delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input insert int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node noob output parameter pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






