C + + Basics-Function pointer array

Source: Internet
Author: User

================================== Declaration ==================================

The copyright of this article belongs to the author.

This article is original, reproduced must be prominently in the text of the author and source, and to ensure the integrity of the article (including this statement).

Do not modify (including this statement) without the authorization of the author, and reserve the right of legal investigation.

Do not use the academic citation without the author's authorization.

Do not use for commercial publishing, commercial printing, commercial references, or other commercial purposes without the authorization of the author.

=======================================================================

I've always felt that the function pointer and the function pointer array Nothing, until later I knew these things

Correct usage, that is to let! You! See! No! Understand!

=======================================================================

Here is my sample small code.

The first version of the typedef.

=======================================================================

function pointers

(Use function pointers as type definitions and initialize arrays)

#include <iostream>using namespace Std;int MyFunc1 (int p,int q) {return p%q;} int MyFunc2 (int p,int q) {return p/q;} int MyFunc3 (int p,int q) {return p-q;} typedef int (*PMF) (int,int); int main () {    PMF arrpmf[3]={myfunc1,myfunc2,myfunc3};    int nres;    NRES=ARRPMF[1] (7,2);    cout<<nres<<endl;    NRES=1[ARRPMF] (7,2);    cout<<nres<<endl;    return 0;}

  

Array of function pointers

#include <iostream>using  namespace std;int MyFunc1 (int p,int q) {return p%q;} int MyFunc2 (int p,int q) {return p/q;} int MyFunc3 (int p,int q) {return p-q;} typedef int (*pmf[]) (int,int); int main () {    PMF arrpmf={myfunc1,myfunc2,myfunc3};    int nres;    NRES=ARRPMF[1] (7,2);            cout<<nres<<endl;    NRES=1[ARRPMF] (7,2);            cout<<nres<<endl;    return 0;}

From know C + + this thing to now barely able to write two lines of code, although over the years I have seen some very fantasy works, but suddenly saw

1[ARRPMF] (7,2);


I've been doing it for a while. Kill anyone is a must for home travel.

=======================================================================

If you do not have a good c/C + + foundation and the basic comprehend by analogy and widely associative small head melon seeds, you can basically "give up the speed of death."

(The line below is not code)

arrpmf[1]==>* (arrpmf+1) <==>* (1+ARRPMF) ==>1[ARRPMF]

=======================================================================

The most anticipated technique (Zuò) (sǐ) edition

Enjoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooy it!

#include <iostream>using  namespace std;int MyFunc1 (int p,int q) {return p%q;} int MyFunc2 (int p,int q) {return p/q;} int MyFunc3 (int p,int q) {return p-q;} Int (*arrpmf[]) (int,int) ={myfunc1,myfunc2,myfunc3};//when the line is 108,000 from the point of call, La La. int main () {    int nres;    NRES=1[ARRPMF] (7,2);//Feel the deep malice of the arrogant universe    cout<<nres<<endl;    return 0;}

  



C + + Basics-Function pointer array

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.