In-depth analysis of C ++ Language Features

Source: Internet
Author: User

C ++, as a language, most people regard it as object-oriented support by default, and think it is an extension of C language, C ++ and C are completely different languages. All the friends who have used the C ++ language think that the C ++ language is indeed a good language.

But sometimes, due to special needs, C ++ has to be used. It is also feasible to implement Thread in C # In C ++, but the code should be slightly complicated:

 
 
  1. //delegate 0  
  2. #define DEFINE_DELEGATE(NAME, R)\  
  3. class NAME##Delegate\  
  4. {\  
  5. public:\  
  6. virtual ~NAME##Delegate(void){ }\  
  7. virtual R operator()(void) = 0;\  
  8. };\  
  9. template\  
  10. class NAME##DelegateImpl : public NAME##Delegate\  
  11. {\  
  12. private:\  
  13. typedef R (C::*F)(void);\  
  14. C* m_class;\  
  15. F m_fun;\  
  16. public:\  
  17. NAME##DelegateImpl(C* c, F f){ m_class = c; m_fun = f; }\  
  18. virtual R operator()(void)\  
  19. {\  
  20. if(m_class)\  
  21. return (m_class-*m_fun)();\  
  22. }\  
  23. };\  
  24. template\  
  25. NAME##Delegate* Make##NAME##Delegate(C* c, F f)\  
  26. {\  
  27. return new NAME##DelegateImpl(c, f);\  
  28. }  
  29. //////////////////////////////////////////////////////////////////////////  
  30. //delegate 1  
  31. #define DEFINE_DELEGATE(NAME, R, P1)\  
  32. class NAME##Delegate\  
  33. {\  
  34. public:\  
  35. virtual ~NAME##Delegate(void){ }\  
  36. virtual R operator()(P1 p1) = 0;\  
  37. };\  
  38. template\  
  39. class NAME##DelegateImpl : public NAME##Delegate\  
  40. {\  
  41. private:\  
  42. typedef R (C::*F)(P1);\  
  43. C* m_class;\  
  44. F m_fun;\  
  45. public:\  
  46. NAME##DelegateImpl(C* c, F f){ m_class = c; m_fun = f; }\  
  47. virtual R operator()(P1 p1)\  
  48. {\  
  49. if(m_class)\  
  50. return (m_class-*m_fun)(p1);\  
  51. }\  
  52. };\  
  53. template\  
  54. NAME##Delegate* Make##NAME##Delegate(C* c, F f)\  
  55. {\  
  56. return new NAME##DelegateImpl(c, f);\  

This example is relatively simple. The C ++ language is used to implement a function. The client is a client program. There are several methods to send notifications to the client when m_sdk is used for processing. One is sending thread messages, and the other is callback, but the traditional callback does not support object-oriented, which is the starting point for implementing delegate. The following is an implementation: class Delegate

 
 
  1. DEFINE_DELEGATE (Open, void, int, string)
  2. Class sdk
  3. {
  4. Public:
  5. OpenDelegate * pEvent;
  6. Sdk (): pEvent (NULL ){}
  7. Void Open ()
  8. {
  9. Cout
  10. If (pEvent! = NULL)
  11. (* PEvent) (100, "hello ");
  12. }
  13. };
  14. Class client
  15. {
  16. Private:
  17. Sdk m_sdk;
  18. Public:
  19. Client ()
  20. {
  21. M_sdk.pEvent=MakeOpenDelegate(This, OnOpen2 );
  22. }
  23. Void Open ()
  24. {
  25. M_sdk.Open ();
  26. }
  27. Void OnOpen ()
  28. {
  29. Cout
  30. }
  31. Void OnOpen2 (int t, string str)
  32. {
  33. Cout
  34. }
  35. };
  1. Differences between standard input implementation methods in C and C ++
  2. How does the C ++ compiler allocate storage space for Const constants?
  3. Basic Conception and method of C ++ Class Library Design
  4. Several Methods for converting C ++ Language
  5. How to better compile C ++ code

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.