C ++ basics -- function pointer array, pointer Array

Source: Internet
Author: User

C ++ basics -- function pointer array, pointer Array

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

This article is copyrighted by the author.

 

This article is original. The author and source must be explicitly indicated in the text, and the integrity of the article (including this statement) must be ensured.

Do not modify (including this statement) without the authority of the author, and reserve the right to pursue the law.

Do not use it for academic reference without the author's authorization.

Do not use it for commercial publishing, commercial printing, commercial reference, or other commercial purposes without the author's authorization.

 

This article is occasionally revised and improved. To ensure that the content is correct, we recommend that you read it in the original article.

Link: http://www.cnblogs.com/wlsandwho/p/4450221.html

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

I have always thought that the function pointer and the function pointer array have nothing to do with these things.

Correct usage, that is, let! You! Look! No! Understand!

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

The following is my sample code.

Let's sacrifice the typedef version.

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

Function pointer

(Use the function pointer as the type definition and initialize the array)

 1 #include <iostream> 2  3 using namespace std; 4  5 int MyFunc1(int p,int q){return p%q;} 6 int MyFunc2(int p,int q){return p/q;} 7 int MyFunc3(int p,int q){return p-q;} 8  9 typedef int (*PMF)(int,int);10 11 int main()12 {13     PMF ArrPMF[3]={MyFunc1,MyFunc2,MyFunc3};14 15     int nRes;16     nRes=ArrPMF[1](7,2);    cout<<nRes<<endl;17     nRes=1[ArrPMF](7,2);    cout<<nRes<<endl;18 19     return 0;20 }

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

Function pointer Array

 1 #include <iostream> 2  3 using  namespace std; 4  5 int MyFunc1(int p,int q){return p%q;} 6 int MyFunc2(int p,int q){return p/q;} 7 int MyFunc3(int p,int q){return p-q;} 8  9 typedef int (*PMF[])(int,int);10 11 int main()12 {13     PMF ArrPMF={MyFunc1,MyFunc2,MyFunc3};14 15     int nRes;16     nRes=ArrPMF[1](7,2);            cout<<nRes<<endl;17     nRes=1[ArrPMF](7,2);            cout<<nRes<<endl;18 19     return 0;20 }

I have barely been able to write two lines of code since I knew about C ++. Although I have seen some mysterious works over the years, I suddenly saw

1[ArrPMF](7,2);

I have been rolling for a while. It is a must-have product for home-based travel to kill people.

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

If you do not have a good C/C ++ Foundation and the Assembly basic outband touch class bypass and widely think of a small head, you can basically "Give up the speed to death.

(The following line is not the code)

ArrPMF[1]==>*(ArrPMF+1)<==>*(1+ArrPMF)<==>1[ArrPMF]

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

Much-anticipated Technology (zu NLP) version

Retry it!

1 # include <iostream> 2 3 4 using namespace std; 5 6 int MyFunc1 (int p, int q) {return p % q;} 7 int MyFunc2 (int p, int q) {return p/q;} 8 int MyFunc3 (int p, int q) {return p-q;} 9 10 int (* ArrPMF []) (int, int) = {MyFunc1, MyFunc2, MyFunc3}; // when this line is 108,000 miles from the call point, la. 11 12 13 int main () 14 {15 int nRes; 16 nRes = 1 [ArrPMF] (7,2 ); // feel the malicious nature of the arrogant universe 17 cout <nRes <endl; 18 19 return 0; 20}

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

Apparently I didn't, so I wrote this blog.

(In fact, these should be in books such as C ++ primer plus .)

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.