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 articleArticle.
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:
- Delegate int FN (int A, int B) // C # Delegate
- 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:
-
- // C #
-
- Delegate int FN (int A, int B );
-
- Class adder {
- Private intC=0;
-
- Public int add (int A, int B ){
-
- Return A + B + C;
-
- }
-
- Public adder (INT c ){This. c= C ;}
-
- }
-
- Class multiplier {
-
- Private intC=0;
-
- Public int multiple (int A, int B ){
- Return a * B * C;
-
- }
-
- Public multiplier (INT c ){This. c= C ;}
-
- }
-
- AdderAdder=NewAdder (1 );
-
- MultiplierMultiplier=NewMultiplier (2 );
-
- FNFN=Adder. Add;
-
- FN (1, 2); // The result is 4
- FN=Multiplier. Multiple;
-
- FN (2, 3); // The result is 12
From aboveCodeTwo problems are described:
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 stateful (stored in the object to which it points). The delegate behavior is not only affected by the input parameters, but also by the status of the target object.
-
- // C ++
-
- Typedef int (* fN) (int A, int B );
-
- Int add (int A, int B ){
-
- Return A + B;
-
- };
-
- Int multiple (int A, int B ){
- Return a * B;
-
- };
-
- Class adder {
-
- Public:
-
- Adder (INT c ){
-
- This->CC= C;
-
- }
-
- Int add (int A, int B ){
-
- Return A + B + C;
-
- }
-
- PRIVATE:
-
- Int C;
- };
-
- Typedef int (adder: * FM) (int A, int B );
-
- Int _ tmain (INT argc, _ tchar * argv [])
-
- {
-
- FNFN=Add;
-
- STD: cout<< FN(1, 2)<< STD: Endl;
- FN=Multiple;
-
- STD: cout<< FN(1, 2)<< STD: Endl;
-
- Adder adder (1 );
-
- FMF= & Adder: Add;
- STD: cout<<(Adder. * f) (1, 2)<< STD: Endl;
-
- 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.