Interpretation of the C ++ callback function code example

Source: Internet
Author: User

The Application of C ++ callback functions is a common operation in actual program development. How can we quickly master this application skill for beginners? Here, I will explain the following code to learn more about the application skills.

Code example of the C ++ callback function:

 
 
  1. #include < string> 
  2. #include < iostream> 
  3. #include < boost/function.hpp> 
  4. using namespace std;  
  5. using namespace boost;  
  6. class Test  
  7. {  
  8. public:  
  9. Test(){};  
  10. virtual ~Test(){};  
  11. void Handle(string& s, unsigned int lines)  
  12. {  
  13. for(int i=0; i<  lines; i++)  
  14. {  
  15. cout < <  s < <  endl;  
  16. }  
  17. };  
  18. };  
  19. template < class T> 
  20. static void CallBack(T& t, boost::function< void 
    (T*, string&, unsigned int)> f)  
  21. {  
  22. string s("test");  
  23. f(&t, s, 3);  
  24. };  
  25. int main()  
  26. {  
  27. Test test;  
  28. CallBack< Test>(test, &Test::Handle);  
  29. return 0;  

The preceding section describes C ++ callback functions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.