zoukankan      html  css  js  c++  java
  • 函数指针_typedef

    一 函数指针

    1 函数类型

         函数的类型由它的返回值类型,和形参类型共通过决定,与函数名无关。

                 eg:bool lengthcompare(const string&, const string&);

                      该函数的类型是bool (const string&, const string&)

    2 函数指针声明

         声明一个上述类型的指针pf

                bool (*pf)(const string&, const string&)

    3 使用函数指针

         (1)把函数名作为一个值使用时,该函数自动转换成指针.

              pf = lengthcompare;

              pf = &lengthcompare;  // 跟上面的语句是等价的,&是可选的

         (2)调用指针指向的函数

              bool b1 = pf("aaa","bbb");

              bool b2 = pf("aaa","bbb");       //等价调用

              bool b3 = lengthcompare("aaa","bbb");   //另一个等价调用

    4 重载的函数指针

         编译器通过指针类型决定选用哪个函数,指针类型必须与重载函数中的某一个精确匹配

                void ff(int*);// 第一个函数

                void ff(unsigned int);// 第二个函数

           

                void (*pf)(unsigned int) = ff;//pf指向的是第二个函数

    5 函数指针的形参

         函数的形参不能是函数类型,但可以是函数指针类型

            void useBigger(int aa,bool pf (const string&, const string&)); // 第三个形参会自动转换为指针

            void useBigger(int aa,bool (*pf) (const string&, const string&)); // 等价声明

    6 返回指向函数的指针

          函数的返回值不能是函数,但可以是函数的指针,返回类型必须显式声明为指针类型

             eg:int(*f1(int))(int*,int);

                     函数f1(int )返回类型为int(*)(int*,int)的指针

    typedef与using(using是c++11开始支持的用法)

         typedef int INTT;----> using INTT = int;

         typedef int* PINTT;----> using PINTT = int*;

         typedef bool Func(int ,int);----> using Func = bool (int,int); // Func是一个函数类型

         typedef bool (*FuncP)(int ,int);----> using FuncP = bool (*)(int,int); // FuncP是一个函数指针类型

  • 相关阅读:
    C++ 类的本质 札记
    eclipse makefile
    boost 简介
    telecom 产品分析js
    javascript 得到页面参数
    ajax 接口统一模式
    备份
    jquery 元素插入详解
    使用WebClient制作一下简单的采集器
    数据库锁机制
  • 原文地址:https://www.cnblogs.com/talenth/p/5807934.html
Copyright © 2011-2022 走看看