Function pointers inside classes

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Function pointers inside classes

 
1
  #1
Apr 18th, 2006
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:
  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:
  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:

  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. };
  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).
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Function pointers inside classes

 
0
  #2
Apr 19th, 2006
"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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Function pointers inside classes

 
0
  #3
Apr 19th, 2006
Good to see that you are back Dave.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Function pointers inside classes

 
1
  #4
Apr 19th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Function pointers inside classes

 
0
  #5
Apr 19th, 2006
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.

Originally Posted by winbatch
Is there an easy answer to my particular example?
Not from me at the moment.

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.
"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
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Function pointers inside classes

 
1
  #6
Apr 19th, 2006
Dave,

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

WB
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Function pointers inside classes

 
1
  #7
Apr 19th, 2006
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.]
"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
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Function pointers inside classes

 
1
  #8
Apr 19th, 2006
[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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 176
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

Re: Function pointers inside classes

 
0
  #9
Apr 20th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Function pointers inside classes

 
1
  #10
Apr 20th, 2006
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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