c++ 小技巧 (五) typedef 定义函数指针以及实例

本文介绍了C++中typedef的应用,包括如何定义整型指针pint_t,结构体data_t及其指针pdata_t,以及如何定义函数指针pfunction_t,并探讨了函数指针数组的概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. typedef int* pint_t;//定义整形指针
2. //定义结构体,定义结构体指针
typedef struct {
    double data;
}data_t,  * pdata_t;
3. //定义函数指针,函数指针数组其实和函数指针差不多,看下面的例子
typedef void (*pfunction_t)();

#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<numeric>
#include<iomanip>
#include<math.h>
#include<bitset>
#include<vector>
#include<stack>
#include<map>
#include<stdio.h>
#include<set>

using namespace std;

double company_perfomance;
double final_performance_1;
double final_performance_2;
double final_performance_3;
double yearlyprofit[] = { 1.00,2.00,3.00,4.00,5.00,6.00 };
const char* friends[] = { "Amy", "Susan", "Alan" };
const char* food[] = { "Sushi Train", "Domino's Pizza", "soul express" };
const char* weekday[] = { "Monday", "Tuesday", "Wednesday"};


//定义普通函数指针调用
typedef void(*method_transport_t)(const char*); //定义了一个新的数据类型,名字是method_transport_t 函数指针类型
typedef double(*money_management_t)(double, double);//定义了一个新的数据类型,名字是money_management_t 函数指针类型  ---上面的方法练习
	
//把一个指针函数当作另外一个函数的参数
void byTrain(const char* name)
{
	printf("travel with %s, go Russian by train!\n", name);
}
void byAirplane(const char* name)
{
	printf("travel with %s, go Singapore by Airplane!\n", name);
}
void byShip(const char* name)
{
	printf("travel with %s, go Thailand by Ship!\n", name);
}
void wayfortravelling(const char* friends[], int n, method_transport_t transportation)//调用上面的指针函数,method_transport_t是一个指针函数,被
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值