Continue to the previous article
#include <iostream>using namespacestd;voidSWAP1 (int&V1,int&v2); typedefvoid(FP_) (int&,int&);//must be defined before FP_ is used!!! voidFuncint&V1,int&v2, Fp_ FP);intMain () {void(*FP) (int&,int&);//Here , FP is a variablecout<<"uninitialized function pointer:"<<fp<<endl;//warning!FP=SWAP1;//initialized intv1=Ten; intV2= -; FP (v1, v2); cout<<v1<<"\ t"<<v2<<Endl; //****************typedefvoid(*FP) (int&,int&);//define FP as a type!FP fp1=0;//FP1 is a variable.cout<<fp1<<endl;//0FP1=SWAP1;//initializedcout<<fp1<<endl;//1FP1 (v1, v2); cout<<v1<<"\ t"<<v2<<Endl;//Fp_ fpx;//Fpx=swap1;//Error//func (v1, v2, FPX);Func (v1, v2, SWAP1);//function name is a variable of function type!!! return 0;}//functionvoidSWAP1 (int&V1,int&v2) { inttmp=v1; V1=v2; V2=tmp;}//formal parameters of a function typevoidFuncint&V1,int&v2, Fp_ FP) {fp (v1,v2);}
A function is a type (determined by the return value and formal parameter list), and the function name is a variable of that type!
A typedef simply takes an individual name for this type, and this type actually exists when the function is defined.
So just pass in the name of the function!
The question is: Why do you define the variable F for the function type, but cannot use the function name (variable take) to assign a value?
C + + function types