C++函数类型

继续上一篇

#include <iostream>

using namespace std;

void swap1(int &v1, int &v2);
typedef void (FP_)(int&, int&);//必须在FP_使用前定义!!!
void func(int &v1, int &v2, FP_ fp);

int main()
{
    void (*fp)(int&,int&);//here, fp is a variable
    cout<<"uninitialized function pointer:"<<fp<<endl; //warning!
    fp=swap1;//initialized

    int v1=10;
    int v2=20;

    fp(v1, v2);
    cout<<v1<<"\t"<<v2<<endl;

    //****************
    typedef void (*FP)(int&, int&);//define FP as a TYPE!
    FP fp1=0; //fp1 is a variable.
    cout<<fp1<<endl;//0
    fp1=swap1; //initialized
    cout<<fp1<<endl;//1

    fp1(v1, v2);
    cout<<v1<<"\t"<<v2<<endl;

//    FP_ fpx;
//    fpx=swap1; //Error
//    func(v1, v2, fpx);
    func(v1, v2, swap1); //函数名就是函数类型的变量!!!

    return 0;
}

//函数
void swap1(int &v1, int &v2){
    int tmp=v1;
    v1=v2;
    v2=tmp;
}
//函数类型的形参
void func(int &v1, int &v2, FP_ fp){
    fp(v1,v2);
}

函数是类型(由返回值和形参列表确定),函数名则是该类型的变量!
typedef只是给这种类型取个别名,而这种类型实际上在定义函数的时候就已经存在了。
  
所以直接传入函数名即可!
  
目前的问题是:为什么定义了函数类型的变量f,却无法使用函数名(变量take)来赋值?

时间: 2024-08-02 00:17:17

C++函数类型的相关文章

swift学习之函数类型

var someInt: Int = 7 Int 就是表示someInt的类型,同理,这个Int也可以换成函数类型,所以也可以像其他类型那样使用函数类型 函数类型主要由三种用途:(一)就是上面说的了 (二)作为参数(三)作为返回类型 再加一个,就是函数也可以嵌套(nested) let math: (Int , Int) -> Int = addTwoInts printMathResult(math, a: 9, b: 9) //printMathResult(math, 9, 9) func

C++数组类型与函数类型

之所以将C++的数组类型与函数类型拿到一块说,是因为两者在很多地方都一样. 首先,声明形式上类似: 数组类型:  type [num]                                          数组:type name[num] 函数类型:  return_type (形参列表)    函数:return_type name(形参列表) 数组指针类型: type (*)[num] 数组指针:type (*name)[num] 函数指针类型: return_type (*)

浅谈swift中的函数类型和闭包

在讲swift的函数类型之前,我们先回忆一下我们以前学的定义一个swift的函数 func add(a: Int,b: Int) -> Int { return a + b } 好了, 我们开始我们函数类型的讲解 上面这个函数的类型是(Int ,Int)->Int 使用函数类型 我们都知道, 在swift中 , 函数类型就像其他数据类型一样,也就意味着我们可以给一个函数的常量或者是变量赋值 var f2: (Int,Int)-> Int = add f2(2,3) //结果为5 好了,接

Swift函数类型

函数可以作为一种类型使用,作为类型与其它数据类型没有区别: 有如下3个函数的定义: (1)func rectangleArea(width : Double, height : Double ) -> Double { let area = width * height return area } (2)func triangleArea(bottom : Double, height : Double ) -> Double { let area = 0.5 * bottom *  heig

明显调用的表达式前的括号必须具有(指针)函数类型 编译器错误 C2064

看到“明显调用的表达式前的括号必须具有(指针)函数类型”这句时我才发现我的语文水平有多烂,怎么看都看不懂,折腾了半天才知道是哪里出了问题. 举个简单的例子 class CTest { void (CTest::*m_pFun)(); void CallFun() { (this->*m_pFun)(); //OK,对象指针和函数名一定要用括号括起来,函数名前面要加上*号 this->*m_pFun(); //error (this->m_pFun)(); //error } //本文链接

Swift - 32 - 函数类型

//: Playground - noun: a place where people can play import UIKit func add(a:Int, b:Int) -> Int { return a + b } // 其中, (Int, Int) -> Int 就是显式的声明函数类型 let anotherAdd:(Int, Int) -> Int = add anotherAdd(3, 4) /*--------------------------------------

C函数类型和函数指针使用方法详解

二.通常的函数调用 一个通常的函数调用的例子: /* 自行包含头文件 */ void MyFun(int x); /* 此处的声明也可写成:void MyFun(int) */ int main(int argc, char* argv[]) {    MyFun(10); /* 这里是调用MyFun(10) 函数 */    return(0); } void MyFun(int x) /* 这里定义一个MyFun函数 */ {    printf("%d\n",x); } 这个My

函数类型

在C和C++中,函数也是一种类型,原因是可以指向函数的指针.这个指针指向了内存中函数的入口处.(多么有趣的现象!因为这一下子把程序和进程的概念似乎又引入进来了!) void (*fPtr)(int );//这是一个变量的定义,指针变量 由于函数是一种类型,我也就可以使用typedef关键字: 对于函数:void function (int a); 有 typedf void FUNCTION (int a);//定义一种函数类型 FUNCTION *p=fPtr;//p是指向上面定义的函数类型的

C++模板:辨别函数类型

在<C++ Template>中有一个辨识函数类型的模板技术,原文的例子貌似有些错误,这里做个对比验证,如有错误,请大家指出,原文的代码如下: template<typename T> class CompoundT { public: enum { IsPtrT = 0, IsRefT = 0, IsArrayT = 0, IsFuncT = 0, IsPtrMemT = 0 }; typedef T BaseT; typedef T BottomT; typedef Compo