C + + function types

Source: Internet
Author: User

Continue to the previous article

#include <iostream>using namespacestd;voidSWAP1 (int&AMP;V1,int&v2); typedefvoid(FP_) (int&,int&);//must be defined before FP_ is used!!! voidFuncint&AMP;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&AMP;V1,int&v2) {    inttmp=v1; V1=v2; V2=tmp;}//formal parameters of a function typevoidFuncint&AMP;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

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.