What is the relationship between the C ++ function pointer and the C # Delegate?

Source: Internet
Author: User

In C #, there is an application method called delegation. Its application methods and implementation functions are quite similar to the C ++ function pointers we have previously introduced. In this article, we will give a brief introduction to these two methods to help you differentiate and understand them.

Both the delegate and function pointer describe the method/function signature and call different implementations through a unified interface. However, the two have obvious differences. Simply put, the delegate object is the real object, and the function pointer variable is only the function entry address. For high-level applications, the flexibility and applicability of delegation are better than those of C ++ function pointers. However, for underlying applications, function pointers are irreplaceable. The following are examples of delegate type and function pointer type definitions:

 
 
  1. Delegate int Fn (int a, int B) // C # delegate
  2. Typedef int (* Fn) (int a, int B) // C ++ function pointer

In terms of form, both the parameter list and return values are the same, but one uses the keyword delegate and the other uses the pointer symbol *. It seems that the "similar" statement is more confident, but it would be too urgent if the two are immediately given an equal sign. Let's verify the difference first:

 
 
  1. // C #
  2. Delegate int Fn (int a, int B );
  3. Class Adder {
  4. Private int c = 0;
  5. Public int Add (int a, int B ){
  6. Return a + B + c;
  7. }
  8. Public Adder (int c) {this. c = c ;}
  9. }
  10. Class Multiplier {
  11. Private int c = 0;
  12. Public int Multiple (int a, int B ){
  13. Return a * B * c;
  14. }
  15. Public Multiplier (int c) {this. c = c ;}
  16. }
  17. Adder adder = new Adder (1 );
  18. Multiplier multiplier = new Multiplier (2 );
  19. Fn fn = adder. Add;
  20. Fn (1, 2); // The result is 4
  21. Fn = multiplier. Multiple;
  22. Fn (2, 3); // The result is 12

The code above illustrates two problems:

1. The delegate object can point to different classes of methods, as long as they comply with the delegate signature;

2. The delegate object is saved in a stateful object.) The delegate behavior is not only affected by the input parameters, but also by the status of the target object.

 
 
  1. //C++  
  2. typedef int(*Fn)(int a, int b);   
  3. int Add(int a, int b) {   
  4. return a + b;   
  5. };   
  6. int Multiple(int a, int b) {   
  7. return a * b;   
  8. };   
  9. class Adder {   
  10. public:   
  11. Adder(int c) {   
  12. this->cc = c;   
  13. }   
  14. int Add(int a, int b) {   
  15. return a + b + c;   
  16. }   
  17. private:   
  18. int c;   
  19. };   
  20. typedef int(Adder::* Fm)(int a, int b);   
  21. int _tmain(int argc, _TCHAR* argv[])   
  22. {   
  23. Fn fn = Add;   
  24. std::cout << fn(1, 2) << std::endl;   
  25. fn = Multiple;   
  26. std::cout << fn(1, 2) << std::endl;   
  27. Adder adder(1);   
  28. Fm f = &Adder::Add;   
  29. std::cout << (adder.*f)(1, 2) << std::endl;   
  30. return 0;   

The delegate in C # is a special object that supports the () operator. This is essentially different from the C/C ++ function pointer, because the C/C ++ function pointer variable does not have the object nature, it is just a simple function entry address. The above Fn can only point to the Add and Multiple common functions, and cannot point to the Add method of the Adder class. Because the signature of the Add method of the Adder class is not int (*) (int a, int B), the compiler automatically adds an implicit this pointer parameter, therefore, its signature is similar to int (*) (Adder * const this, int a, int B.

If you want to direct the pointer to a member function, you need to add a type qualifier in the form of typedef int (Adder: * Fm) (int a, int B. Therefore, the C ++ function pointer cannot direct to different classes of methods like the C # Delegate; it does not have the State nature of the object; in use, the function pointer is not as flexible as the delegate. Therefore, when we hear the saying "delegate is similar to the C/C ++ function pointer", we should understand its similarities and differences.
 

Related Article

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.