1, the first address is assigned to the function name, so the function name is the first address of the function memory address.
2, a pointer to a function | You must ensure that the function is defined |
| and allocated Memory | Otherwise he will point to an empty address, which is the big bogey of pointers.
3,long (*func) (int): A pointer is declared that points to a function.
LONG*FUNC1 (int): A function is declared that returns a pointer.
4, Definition:
Float (*FP) (float&, float&);
fp=&rectangle;
All right
#include <iostream>#include<string>using namespacestd;float(*FP) (float&,float&);void(*p) (float&,float&);//Function call list, will greatly reduce the amount of code floatRectanglefloat&x,float&y) { returnX *y;}floatTrianglefloat&x,float&y) { returnX * y *0.5;}voidSwap (float&x,float&y) { floatn =x; X=y; Y=N;}voidPrintfloat&x,float&y) {cout<<"length as:"<< x <<"width is:"<< y <<Endl;}BOOLCheckstringstr) { for(intI=0; I<str.length (); i++) { if((Str[i) >'9'|| Str[i] <'0') && (str[i]!='.')) { return false; } } return true;}void Get(float&x,float&y) {cout<<"Please enter a new value for x:"<<Endl; STD::stringstr1; CIN>>str1; while(!Check (STR1)) {cout<<"input is not a number, please re-enter: x="<<Endl; CIN>>str1; } cout<<"Please enter a new value for Y:"<<Endl; STD::stringstr2; CIN>>str2; while(!Check (STR2)) {cout<<"input is not a number, please re-enter: y="<<Endl; CIN>>str2; } x=atof (Str1.c_str ()); Y=atof (Str2.c_str ());}intMain () {BOOLquit=false; floatA =2, b=3; intchoice; while(quit==false) {cout<<"0:quit 1:input x, y 2 triangle 3 Rectangle 4 swap x y"<<Endl; CIN>>choice; Switch(choice) { Case 1: cout<<"set the value before long and wide"; /*print (A, b); Get (A, b); cout << "long and wide values after setting"; Print (A, b);*/P=Get; Break; Case 2: //print (A, b);cout <<"the area of the triangle is:";/*<< Triangle (A, b) << Endl;*/FP=triangle; Break; Case 3: //print (A, b);cout <<"the area of the rectangle is:";/*<< Rectangle (A, b) << Endl;*/FP=Rectangle; Break; Case 4: cout<<"value of long and wide before swapping"; /*print (A, b); Swap (A, b); cout << "long and wide values after swapping"; Print (A, b);*/P=Swap; Break; default: Quit=true; Break; } if(choice==1|| choice==4) {print (A, b); P (A, b); Print (A, b); } if(choice==2|| choice==3) {print (A, b); cout<<"the area is:"<< FP (A, B) <<Endl; } }}
17: Special class Member: function pointer 5