zoukankan      html  css  js  c++  java
  • C++之指向函数的指针

     1 #include <iostream>
     2 #include <string>
     3 //#include "Sales_item.h"
     4 #include "Sales_item.cpp"
     5 
     6 using  namespace std;
     7 //使用typedef简化定义
     8 typedef bool (*pf)(const string &s1,const string &s2);
     9 typedef int (*PF)(int *, int);
    10 
    11 
    12 bool length(const string &s1,const string &s2)
    13 {
    14     return s1.size()==s2.size();
    15 }
    16 
    17 void useBigger(const string &s1,const string &s2,bool (*pf)(const string &s1,const string &s2))
    18 {
    19     cout<<length(s1,s2)<<endl;
    20 }
    21 //ff是一个函数名 有一个形参 x 返回的结果为一个函数指针int(*)(int *,int)
    22 //int (*ff(int x))(int *, int)
    23 //{
    24 //
    25 //}
    26 //等同于上边的定义
    27 //PF(*ff(int x))
    28 //{
    29 //
    30 //}
    31 
    32 int main()
    33 {
    34     //指向bool类型的函数指针
    35     //bool (*pf)(const string &,const string &);
    36     pf pf1; //必须指向同类型的函数
    37     //pf=&length;
    38     pf pf2;
    39     cout<<length("hello","pointer")<<endl;
    40     cout<<(*pf1)("hello","pointer")<<endl;
    41     useBigger("hello","pointer",pf2);
    42     return 0;
    43     //预处理进行调试
    44     #ifdef NDEBUG
    45     cout<<""<<endl;
    46     #endif // NDEBUG
    47 }
  • 相关阅读:
    HTML特效代码大全
    PHP网站加网站访问量统计
    定时显示隐藏
    加入收藏 设为首页
    IP和归属地
    手机站的拨打电话和发短信
    Shell运算
    Shell命令替换与变量替换
    $* 和 $@ 的区别
    Shell特殊变量列表
  • 原文地址:https://www.cnblogs.com/yh2924/p/12590859.html
Copyright © 2011-2022 走看看