欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

C++中函數(shù)指針詳解及代碼分享

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語言|點(diǎn)擊: 次

函數(shù)指針

函數(shù)存放在內(nèi)存的代碼區(qū)域內(nèi),它們同樣有地址。如果我們有一個(gè)int test(int a)的函數(shù),那么,它的地址就是函數(shù)的名字,如同數(shù)組的名字就是數(shù)組的起始地址。

1、函數(shù)指針的定義方式:data_types (*func_pointer)( data_types arg1, data_types arg2, ...,data_types argn);
c語言函數(shù)指針的定義形式:返回類型 (*函數(shù)指針名稱)(參數(shù)類型,參數(shù)類型,參數(shù)類型,…);
c++函數(shù)指針的定義形式:返回類型 (類名稱::*函數(shù)成員名稱)(參數(shù)類型,參數(shù)類型,參數(shù)類型,….);   

例如: int (*fp)(int a); //這里就定義了一個(gè)指向函數(shù)(這個(gè)函數(shù)參數(shù)僅僅為一個(gè)int類型,函數(shù)返回值是int類型)的指針fp。
類成員函數(shù)指針與普通函數(shù)指針不是一碼事。前者要用.*與->*運(yùn)算符來使用,而后者可以用*運(yùn)算符(稱為“解引用”dereference,或稱“間址”indirection)。

普通函數(shù)指針實(shí)際上保存的是函數(shù)體的開始地址,因此也稱“代碼指針”,以區(qū)別于C/C++最常用的數(shù)據(jù)指針。

而類成員函數(shù)指針就不僅僅是類成員函數(shù)的內(nèi)存起始地址,還需要能解決因?yàn)镃++的多重繼承、虛繼承而帶來的類實(shí)例地址的調(diào)整問題,所以類成員函數(shù)指針在調(diào)用的時(shí)候一定要傳入類實(shí)例對象。

函數(shù)指針示例

#include <stdio.h>
#include <stdlib.h>
int fun1()
{
  printf("this is fun1 call\n");
  return 1;
}
void fun2(int k, char c)
{
  printf("this is fun2 call:%d %c\n", k, c);
}
int main()
{
  int (*pfun1)() = NULL;
  void (*pfun2)(int, char) = NULL;
  int a,b;
  pfun1 = fun1; //第一種賦值方法
  a = pfun1(); //第一種調(diào)用方法(推薦)
  printf("%d\n",a);
  b = (*pfun1)();//第二種調(diào)用方法
  printf("%d\n",b);
  pfun2 = &fun2;//第二種賦值方法(推薦,因?yàn)楹推渌麛?shù)據(jù)指針賦值方法一致)
  pfun2(1,'a');
  (*pfun2)(2,'b');
  return 0;
}

函數(shù)指針作為函數(shù)參數(shù):

#include <stdio.h>
#include <stdlib.h>
void fun(int k, char c)
{
  printf("this is fun2 call:%d %c\n", k, c);
}
void fun1(void (*pfun)(int, char), int a, char c)
{
  pfun(a, c);
}
int main()
{
  fun1(fun, 1, 'a');
  return 0;
}
// c++ 的形式差不多

函數(shù)指針作為函數(shù)返回值:

// c 形式
#include <stdio.h>
#include <stdlib.h>
void fun(int k, char c)
{
  printf("this is fun2 call:%d %c\n", k, c);
}
//fun1 函數(shù)的參數(shù)為double,返回值為函數(shù)指針void(*)(int, char)
void (*fun1(double d))(int, char)
{
  printf("%f\n",d);
  return fun;
}
int main()
{
  void (*p)(int, char) = fun1(3.33);
  p(1, 'a');
  return 0;
}
//c++ 形式
#include <iostream>
using namespace std;
class test
{
public:
  int fun(int a, char c)
  {
    cout<<"this is fun call:"<<a<<" "<<c<<endl;
    return a;
  }
};
class test2
{
  public:
  // test2 的成員函數(shù)fun1,參數(shù)是double,
  //返回值是test的成員函數(shù)指針int(test::*)(int, char)
  int (test::*fun1(double d))(int, char)
  {
    cout<<d<<endl;
    return &test::fun;
  }
};
int main()
{
  test mytest;
  test2 mytest2;
  int (test::*p)(int, char) = mytest2.fun1(3.33);
  (mytest.*p)(1, 'a');
  return 0;
}

函數(shù)指針數(shù)組:

#include <stdio.h>
#include <stdlib.h>
float add(float a,float b){return a+b;}
float minu(float a,float b){return a-b;}
int main()
{
  //定義一個(gè)函數(shù)指針數(shù)組,大小為2
  //里面存放float (*)(float, float)類型的指針
  float (*pfunArry[2])(float, float) = {&add, &minu};
  double k = pfunArry[0](3.33,2.22);// 調(diào)用
  printf("%f\n", k);
  k = pfunArry[1](3.33,2.22);
  printf("%f\n", k);
  return 0;
}
//c++ 可類比

typedef 簡化函數(shù)指針類型:

#include <stdio.h>
#include <stdlib.h>
float add(float a,float b)
{
  printf("%f\n",a+b);
  return a+b;
}
float minu(float a,float b)
{
  printf("%f\n",a-b);
  return a-b;
}
//用pfunType 來表示float(*)(float, float)
typedef float(*pfunType)(float, float);
int main()
{
  pfunType p = &add;//定義函數(shù)指針變量
  p(3.33, 2.22);
  pfunType parry[2] = {&add, &minu};//定義函數(shù)指針數(shù)組
  parry[1](3.33, 2.22);
  //函數(shù)指針作為參數(shù)可以定義為:void fun(pfunType p)
  //函數(shù)指針作為返回值可以定義為:pfunType fun();
  return 0;
}
//c++ 可類比

總結(jié)

以上就是本文關(guān)于C++中函數(shù)指針詳解及代碼示例的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:C語言實(shí)現(xiàn)的學(xué)生選課系統(tǒng)代碼分享等,有什么問題可以隨時(shí)留言,小編會(huì)及時(shí)回復(fù)大家的。

上一篇:詳解C++中String類模擬實(shí)現(xiàn)以及深拷貝淺拷貝

欄    目:C語言

下一篇:C/C++ 動(dòng)態(tài)數(shù)組的創(chuàng)建的實(shí)例詳解

本文標(biāo)題:C++中函數(shù)指針詳解及代碼分享

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1067.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有