943,696 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5438
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 18th, 2006
1

Function pointers inside classes

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3. class Class1
  4. {
  5. public:
  6. void callBack( string source, void (*myFunc) (string))
  7. {
  8. cout<<"About to do call back..."<<endl;
  9. myFunc( source );
  10. }
  11. };
Classes.cpp:
C++ Syntax (Toggle Plain Text)
  1. #include "Classes.h"
  2. void thisWorks(string works )
  3. {
  4. cout<<"THIS ONE WORKS"<<works<<endl;
  5. }
  6. int main()
  7. {
  8. Class1 c1;
  9. c1.callBack( "TESTING", &thisWorks );
  10. return 0;
  11. }
./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:

C++ Syntax (Toggle Plain Text)
  1. Classes.h
  2. #include <iostream>
  3. using namespace std;
  4. class Class1
  5. {
  6. public:
  7. void callBack( string source, void (*myFunc) (string))
  8. {
  9. cout<<"About to do call back..."<<endl;
  10. myFunc( source );
  11. }
  12. };
  13. class Class2
  14. {
  15. public:
  16. Class2( string myString)
  17. {
  18. Class1 class1;
  19. class1.callBack( myString, &test );
  20. }
  21. void test( string whatever )
  22. {
  23. cout<<"Call back function called!"<<whatever<<endl;
  24. }
  25. };
C++ Syntax (Toggle Plain Text)
  1. Classes.cpp
  2. #include "Classes.h"
  3. int main()
  4. {
  5. Class2 class2("TESTING" );
  6. return 0;
  7. }
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).
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Apr 19th, 2006
0

Re: Function pointers inside classes

Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Apr 19th, 2006
0

Re: Function pointers inside classes

Good to see that you are back Dave.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Apr 19th, 2006
1

Re: Function pointers inside classes

Quote originally posted by Dave Sinkula ...
http://www.parashift.com/c++-faq-lit...o-members.html
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
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Apr 19th, 2006
0

Re: Function pointers inside classes

Quote originally posted by winbatch ...
Dave,

I was looking at that - went over my head.
Mine as well, too much of the time. I can't even recall some similar stuff elseweb that I contributed to fairly recently.

Quote originally posted by winbatch ...
Is there an easy answer to my particular example?
Not from me at the moment.

Quote 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...
Member functions are not like regular functions. So pointer to member functions are not like pointers to regular functions. The implicit this makes things different... or ugly... or difficult.

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Apr 19th, 2006
1

Re: Function pointers inside classes

Dave,

Don't lose sleep over it. But if you're up anyway, yes please find the other thread

WB
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Apr 19th, 2006
1

Re: Function pointers inside classes

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.]
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Apr 19th, 2006
1

Re: Function pointers inside classes

Quote ...
[It seems my edit may have been after your reply, so I un-edited it and replied with that edit. Apologies -- continuity.]
Say what Dr. Dave?

I had trouble following your other thread too. guess I'm biting off more than I can chew.
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Apr 20th, 2006
0

Re: Function pointers inside classes

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
Reputation Points: 39
Solved Threads: 24
Junior Poster
dubeyprateek is offline Offline
176 posts
since Mar 2006
Apr 20th, 2006
1

Re: Function pointers inside classes

Quote 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
That works, unfortunately, I don't want it to be a static function as I want it to use the instance of the class that I'm working in.
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Can someone please help me with Binary Addtion.
Next Thread in C++ Forum Timeline: Need Help in Reading characters from a text file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC