zoukankan      html  css  js  c++  java
  • C++_函数指针的内容和应用

    函数

    01.基本情况
    	声明
    	定义
    	调用 
    声明: 原型 prototype
    定义 -->>函数名
         -->> 输入参数列表: 函数指针  重载  函数模板-泛型 默认参数
    	 -->> 函数体:  局部静态变量 局部非静态变量
    	 -->> 返回值: 基本类型 指针  返回一个变量的引用  void  无返回值 
    调用:
      -->实参 形参
      -->传值 传址 传引用
      -->函数指针
    其他:
        成员函数--静态成员函数和非静态成员函数
    	构造函数 析构函数
    	lambda表达式
    	函数对象
    常见情况
     01.函数返回多个参数
       1.第一种是将把返回值打包返回,如返回一个数组名,指针,结构体.,
            例如:定义结构体,返回该结构体指针-返回指针的方式
     	   有返回值的函数可以改成void()型函数
       2. 第二种方法
     	  利用函数的副作用, 返回值在函数外定义, 在函数内修改, 一般为void函数
     
     02.数组指针-数组引用
       数组会自动转化为指针,将数组形参可声明为数组的引用,
       如果形参是数组的引用,编译器会传递数组的引用本身
    

    函数指针

     1.声明函数指针
         指针是变量,所以函数指针也是变量,因此可以使用变量定义的方式来定义函数指针	
          普通  int * pa;	
          函数指针:  (1)函数的参数个数(2)函数的参数类型(3)函数的返回值类型
                       指针是变量,所以函数指针也是变量	  
          声明方式:
                (1)返回类型 (*函数指针名称)(参数类型,参数类型,参数类型,…);-不加括号就会产生二义性
                    int (*pFunction)(float,char,char);		
    
                (2)返回类型 (类名称::*函数成员名称)(参数类型,参数类型,参数类型,….)--静态成员函数和非静态成员函数
                   	int (MyClass::*pConstMemberFunction)(float,char,char)  const
    			    int (MyClass::*pMemberFunction)(float,char,char)
     2.赋值和调用
    
      int func1(float f,int a,int b){
           return f*a/b;
           }
       int func2(float f,int a,int b){
            return f*a*b
       	}
      
      // 取地址符号是可选的(1)避免二义性(2)形式一致性。在普通指针赋值,需要加取地址符号是为了区别于将地址还是将内容赋给指针
       int (*pFunction)(float,char,char);
       
       pFunction=func1;
       pFunction=&func2;
       //使用函数指针调用函数的方式-上 这两种使用函数指针调用函数的方式都可
        pFunction(10.0,’a’,’b’);
      (*pFunction)(10.0,’a’,’b’);
    

    具体示例

    01.概念示意
     # (1)一个函数指针可以多次赋值(想想C++中的引用)(2)取地址符号是可选的,却是推荐使用的
     #include <iostream>
     using namespace std;
         int func1( int a,int b){
     	    return a>=b? a:b;
     	    }
     	int func2(int a,int b){
     	     return a<b? a:b;
     		}
        int main(){
             // 1.声明函数指针
          int (*pFunction)(int,int);
     	 	 // 2.赋值
     	 pFunction=func1;
     	 	 // 3.调用
     	int a = 2;
     	int b = 3;
     	 int c = pFunction(a,b);
     	 std::cout << c << "
    ";
          pFunction=&func2;
         c=(*pFunction)(a,b);
     	std::cout << c;
         }
     	
    02.编码示意 函数指针作为形参,函数作为实参,函数作为实参使用时,会自动转换为函数指针
        #include <iostream>
        using namespace std;
        int func1( int a,int b){
    	    return a>=b? a:b;
    	    }
    	int func2(int a,int b){
    	     return a<b? a:b;
    		}
    	//函数指针
    	int call_Function_pointer(int (*pFunction)(int ,int ),int x, int y){
    	     int result=pFunction(x,y); 
             std::cout << result << "
    "; 
             return result;
    	    }
    	// 	函数指针数组
    	int(*func_pointers[2])(int,int)={func1,func2}; 
       int main(){
    	/// 函数指针的赋值语句的等号换成了形参和实参结合的模式
    	int a =22;
    	int b = 33;
    	call_Function_pointer(func1,a,b);
    	call_Function_pointer(&func2,a,b);
    	
     	int xa =55;
    	int yb = 66;
        for(int i=0;i<2;i++){  
         std::cout <<func_pointers[i](xa,yb) << "
    ";
          }  
    	}
    	
    #说明: 类似于R语言中,apply(b,1,sum)  代表对矩阵b进行行计算,分别对每一行进行求和 1——表示按行计算,2——按列计算
        Java中的高阶函数,高阶函数是指接受另外一个函数作为参数,或返回一个函数的函数
    	spark或者Flink
        .flatMap(new FlatMapFunction<String, WordWithCount>() {
                    @Override
                     public void flatMap(String value, Collector<WordWithCount> out){}
    	}
    

    指针函数

    01.概念解释   
      整型    作为返回   int func(){}
      整型指针作为返回值 int * func(){}
      函数指针作为返回值  float (* func1(char op) ) (float ,float)
         函数指针对应的函数是返回值为浮点型,带有两个参数,两个参数都是浮点型  float (*)(float ,float )
    	 float (* func1(char op) ) (float ,float)
    	 其名称为func,其参数的个数为1个;
         其各个参数的类型为:op—char;
    	 其返回变量(函数指针)类型为:float(*)(float,float)
    指针函数
         返回指针的函数,一个函数,它的返回值是指针	
    	 虽然不能返回一个函数,但是能返回执行函数类型的指针
    

    函数指针和指针函数同时存在

     一个参数类型为函数指针,返回值还是函数指
       void (*signal)(int signo,void (*func)(int)))(int);
         其函数名称为:signal
         其参数个数为:2
         其各个参数的类型为:signo--int, func— void (*)(int)
         其返回的变量(函数指针)的类型为:void(*)(int)
    

    函数和其他关系

     1.函数指针数组
       float(* pFunctionArray[10])(float,float)
     2.函数指针  定义新的类型、
        typedef char* PCHAR;  
        PCHAR pa, pb; // 可行,同时声明了两个指向字符变量的指针	
        typedef 已知类型 新类型;	
     3.数组-与指针
      指针数组-- 一个数组,它包含的元素是指针
      数组指针-- 一个指针,它指向一个数组
     4计算机语言学习
      1)语法过于复杂
      2)语义过于复杂。
      从哲学上讲,可以对应为 1)形式过于复杂。 2)内容过于复杂。
    

    参考

      嗯,让我们彻底搞懂C/C++函数指针吧(一 https://blog.51cto.com/hipercomer/792300
      嗯,让我们彻底搞懂C/C++函数指针吧(二 https://blog.51cto.com/hipercomer/792301
      The Function Pointer Tutorials http://www.newty.de/fpt/index.html
      c,c++函数返回多个值的方法 https://www.cnblogs.com/anwcq/p/C_hanshu.html
  • 相关阅读:
    redis 数据类型 Hash
    redis有序集合类型sort set
    redis数据类型set
    redis的 list
    redis的key
    centos安装redis
    input聚焦事件
    width(),innerWidth(),outerWidth(),outerWidth(true)
    jq 选择器
    详解CSS中:nth-child的用法_大前端
  • 原文地址:https://www.cnblogs.com/ytwang/p/15337877.html
Copyright © 2011-2022 走看看