| | |
Function pointers inside classes
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
So I'm trying to learn how to use function pointers (ie to pass the address of a function to another function to execute it - like a call back). I have it working when I use it as follows:
Classes.h:
Classes.cpp:
./Classes
About to do call back...
THIS ONE WORKSTESTING
However, I would like to figure out how to get it to work where I don't have any 'standalone/global' functions, just classes:
However, when I compile it, I get:
"Classes.h", line 18: Error: Formal argument myFunc of type void(*)(std::basic_string<char, std::char_traits<char>, std::allocator<char>>) in call to Class1::callBack(std::basic_string<char, std::char_traits<char>, std::allocator<char>>, void(*)(std::basic_string<char, std::char_traits<char>, std::allocator<char>>)) is being passed void(Class2:
)(std::basic_string<char, std::char_traits<char>, std::allocator<char>>).
Based on some googling, I'm pretty sure I have to create a typedef to void of my class, but I'm a bit confused. (Note I have a much larger example, the above is to try and illustrate just the problem).
Classes.h:
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; class Class1 { public: void callBack( string source, void (*myFunc) (string)) { cout<<"About to do call back..."<<endl; myFunc( source ); } };
C++ Syntax (Toggle Plain Text)
#include "Classes.h" void thisWorks(string works ) { cout<<"THIS ONE WORKS"<<works<<endl; } int main() { Class1 c1; c1.callBack( "TESTING", &thisWorks ); return 0; }
About to do call back...
THIS ONE WORKSTESTING
However, I would like to figure out how to get it to work where I don't have any 'standalone/global' functions, just classes:
C++ Syntax (Toggle Plain Text)
Classes.h #include <iostream> using namespace std; class Class1 { public: void callBack( string source, void (*myFunc) (string)) { cout<<"About to do call back..."<<endl; myFunc( source ); } }; class Class2 { public: Class2( string myString) { Class1 class1; class1.callBack( myString, &test ); } void test( string whatever ) { cout<<"Call back function called!"<<whatever<<endl; } };
C++ Syntax (Toggle Plain Text)
Classes.cpp #include "Classes.h" int main() { Class2 class2("TESTING" ); return 0; }
"Classes.h", line 18: Error: Formal argument myFunc of type void(*)(std::basic_string<char, std::char_traits<char>, std::allocator<char>>) in call to Class1::callBack(std::basic_string<char, std::char_traits<char>, std::allocator<char>>, void(*)(std::basic_string<char, std::char_traits<char>, std::allocator<char>>)) is being passed void(Class2:
)(std::basic_string<char, std::char_traits<char>, std::allocator<char>>).Based on some googling, I'm pretty sure I have to create a typedef to void of my class, but I'm a bit confused. (Note I have a much larger example, the above is to try and illustrate just the problem).
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Dave,
I was looking at that - went over my head. Is there an easy answer to my particular example? Part of the problem seems to be that I'm INSIDE my class when I'm trying to pass another member function to the other class. In all the other examples, I'm in main() and I'm trying to pass a member of a class (rather than being inside the class itself..) sorry if I can't convey this clearly...
Winbatch
I was looking at that - went over my head. Is there an easy answer to my particular example? Part of the problem seems to be that I'm INSIDE my class when I'm trying to pass another member function to the other class. In all the other examples, I'm in main() and I'm trying to pass a member of a class (rather than being inside the class itself..) sorry if I can't convey this clearly...
Winbatch
•
•
•
•
Originally Posted by winbatch
Dave,
I was looking at that - went over my head.
•
•
•
•
Originally Posted by winbatch
Is there an easy answer to my particular example?
•
•
•
•
Originally Posted by winbatch
Part of the problem seems to be that I'm INSIDE my class when I'm trying to pass another member function to the other class. In all the other examples, I'm in main() and I'm trying to pass a member of a class (rather than being inside the class itself..) sorry if I can't convey this clearly...
I may try to find that other thread. But I know I struggled with that FAQ for quite a while before I posted anything that looked useful.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
http://cboard.cprogramming.com/showthread.php?t=77687
Seems as though I basically punted.
[It seems my edit may have been after your reply, so I un-edited it and replied with that edit. Apologies -- continuity.]
Seems as though I basically punted.
[It seems my edit may have been after your reply, so I un-edited it and replied with that edit. Apologies -- continuity.]
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
•
•
Originally Posted by dubeyprateek
u are doing fine, as far as i can see, just declare test as static, next while passing a function u need not give &, and pass test like Class2::test in the callback funtion.... function
![]() |
Other Threads in the C++ Forum
- Previous Thread: Can someone please help me with Binary Addtion.
- Next Thread: Need Help in Reading characters from a text file
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector visualstudio win32 windows winsock word wordfrequency wxwidgets







