c++函數指針


函數指針用於指向一個函數,函數名是函數體的入口地址

#define _CRT_SECURE_NO_WARNINGS #include<iostream>


using namespace std; int func(int a, int b) { cout << "1999年寫的func" << endl; return 0; } int func2(int a, int b) { cout << "1999年寫的func2" << endl; } int func3(int a, int b) { cout << "1999年寫的func3" << endl; } //2018年想添加一個新的子業務
int func2018(int a, int b) { cout << "2018年寫的func2018" << endl; } /* 方法一: 函數的返回值,函數的參數列表(形參的個數,類型,順序) 定義一個函數類型 */ typedef int (FUNC)(int, int); //定義一個統一的接口將他們都調用起來
void my_function(int(*fp)(int, int),int a,int b) { cout << "1999年的實現這個架構業務" << endl; cout << "固定業務1" << endl; cout << "固定業務2" << endl; fp(a, b); cout << "固定業務3" << endl; } /* 方法二: 定義一個函數指針 */ typedef int(*FUNC_P)(int, int); int main() { #if 0
    //方法一:
    FUNC *fp = NULL; fp = func; fp(10, 20); //等價 (*fp)(10,20); //方法二
    FUNC_P fp2 = NULL; fp2(100, 200); //方法三
    int(*fp3)(int, int) = NULL; #endif my_function(func, 10, 20); my_function(func2, 100, 200); my_function(func3, 1000, 2000); my_function(func2018, 2018, 2018); return 0; }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM