// ================================================ ====================================
// Title:
// C ++ vs C # (7): pointer to and delegate to a function
// Author:
// Norains
// Date:
// Thursday 17-jaruary-2011
// Environment:
// Visual Studio 2010
// Visual Studio 2005
// Modify
// AM, Thursday 17-January-2011 first draft
// Pm, Thursday 17-January-2011 modify the method of C ++ call
// ================================================ ====================================
1. pointer and delegate to the Function
For example:
Static int multiply (INT param1, int param2) <br/>{< br/> return param1 * param2; <br/>}</P> <p> static int divide (INT param1, int param2) <br/>{< br/> return param1/param2; <br/>}< br/>
Now I don't want to call these two functions directly, but indirectly call them in some way. This seems to be a bit difficult to say. If it is not straightforward, let's say it another way. Suppose we have a container that can obtain information about functions, but we don't have to worry about calling functions. We just need to call the container and let the container select the corresponding function.
This so-called container exists in both C ++ and C. Then, we start with C ++.
In C ++, the container name is: pointer to a function. Yes, it's a pointer. Pointers are definitely the essence of C ++, just like the gold oil, where they are put is truth. However, the pointer here points to the function, so the Declaration seems a little weird. The Code is as follows:
// Define two variables. Use <br/> int A = 10; <br/> int B = 20; </P> <p> // declare a pointer to a function <br/> int (* pfunc) (INT, INT ); </P> <p> // assign the function address to the pointer. <br/> If (bdiv! = False) <br/>{< br/> pfunc = divide; <br/>}< br/> else <br/>{< br/> pfunc = multiply; <br/>}</P> <p> // call the pointing function <br/> pfunc (A, B ); </P> <p> // call can also use this form <br/> (* pfunc) (a, B); <br/>
Assignment is easy to understand. It is no different from normal assignment. It is not difficult to understand the call. Simply put, it is equivalent to multiply (A, B ), A complicated addition * can also be understood as a function for getting Pointer Points. The declared statement: int (* pfunc) (INT, INT). How can this problem be solved? No way. We cannot change this because the standards are fixed. However, C ++ is interesting here. You can use typedef to make the statement clearer! Believe it or not, let's take a look:
// Define a function pointer type, and its name is func <br/> typedef int (* func) (INT, INT ); </P> <p> // declare a pointer to the function <br/> func pfunc; <br/>
Is such a statement clearer than the previous one? Of course, the cost is an extra line of typedef code.
So far, the description of C ++ has come to an end. Let's take a look at C #. In C #, there is no pointer, and naturally there will be no pointer pointing to a function, instead of a delegate. The delegate Declaration is very similar to a function, but it does not contain the function body and requires the delegate keyword. If implemented in C #, the Code is as follows:
// Define a delegate funcdelegate, whose return type and form are the same as those in multiply and divide <br/> delegate int funcdelegate (INT param1, int param2 ); </P> <p> // declare two variables for Function Definition <br/> int A = 10; <br/> int B = 20; </P> <p> // declare a delegate variable <br/> funcdelegate func; </P> <p> // assign a value to the delegate variable <br/> If (bdiv! = False) <br/>{< br/> func = new funcdelegate (divide ); <br/>}< br/> else <br/>{< br/> func = new funcdelegate (multiply ); <br/>}</P> <p> // call the corresponding function <br/> func (a, B); <br/>
C # uses the delegate method. In fact, it is very similar to C ++'s typedef. Both of them must first define a type and then declare a variable using this type. In the assignment phase, C ++ simply assigns the function address to the pointer, while C # must declare an object with new, in addition, the corresponding function is required as the form parameter. At this stage, C # seems more complex. In the call phase, both C ++ and C # can be called by adding a bracket after the container, but C ++ has another * method. Although this method is a little tricky, after all, it is a pleasure. Why not?