zoukankan      html  css  js  c++  java
  • 23)函数重载和函数指针

    1)第一中方式

      我想调用我自己定义的函数,但是  我不想用我的函数名字,那么就可以这样

         

    2)第二种方式:  

        定义一种指向这种函数类型的指针就可以

        typedef int(*MY_FUNC_P)(int,int)--->这个MY_FUNC_P就是指向返回值类型是int,参数列表是(int,int)的函数指针

        然后我调用:

        MY_FUNC_P fp=NULL;

        fp=func;

        fp(10,20)  这样就可以。

        

     1 #include<iostream>
     2 int func(int a,int b)
     3 {
     4     printf("dajldk
    ");
     5     return 1;
     6 }
     7 int fun(int a,int b)
     8 {
     9     printf("djaslfd
    ");
    10     return 100;
    11 }
    12 typedef int (*MY_FUNC_P)(int ,int);
    13 int main()
    14 {
    15     MY_FUNC_P fp=func;
    16     fp(100,100);
    17     printf("@@@@@@@@@@@@@@@@@@@@@
    ");
    18     fp=fun;
    19     fp(10,100);
    20 
    21     
    22     return 0;
    23 }

    3)第三种形式:

        

    1 int main()
    2 {
    3     int (*fp)(int ,int)=NULL;
    4     fp=func;
    5     fp(100,100);
    6 }

    4)我加了重载,里面会有一些东西强化

        

     1 #include<iostream>
     2 int func(int a,int b)
     3 {
     4     printf("dajldk
    ");
     5     return 1;
     6 }
     7 //很明显,这两个函数重载了,
     8 int func(int a,int b,int c)
     9 {
    10     printf("dajldk
    ");
    11     return 1;
    12 }
    13 int fun(int a,int b)
    14 {
    15     printf("djaslfd
    ");
    16     return 100;
    17 }
    18 typedef int (*MY_FUNC_P)(int ,int);
    19 int main()
    20 {
    21     MY_FUNC_P fp=func;//此时的这个fp-->func(int,int),和那个func(int,int,int)美关系
    22     fp(100,100);//这个也是调用那个func(int,int),不会因为重载去调用那个func(int,int,int),因为fp指向的函数入口是func(int,int)而不是func(int,int,int)
    23     printf("@@@@@@@@@@@@@@@@@@@@@
    ");
    24     fp=fun;
    25     fp(10,100);
    26 
    27     
    28     return 0;
    29 }

    5)也就是说,函数指针是严格匹配的,必须函数列表严格对应

  • 相关阅读:
    nyoj 230/poj 2513 彩色棒 并查集+字典树+欧拉回路
    nyoj 211 Cow Contest
    nyoj 203 三国志 dijkstra+01背包
    nyoj 170 网络的可靠性
    nyoj 120 校园网络
    nyoj 115 城市平乱 dijkstra最短路
    nyoj 42 一笔画问题 欧拉路径
    nyoj 38 布线问题
    hdu 2089 不要62--数位dp入门
    nyoj 712 探 寻 宝 藏--最小费用最大流
  • 原文地址:https://www.cnblogs.com/xiaoyoucai/p/8179274.html
Copyright © 2011-2022 走看看