函数指针学习(还没明白具体含义难点)
来源:互联网 发布:湄公河惨案 知乎 编辑:程序博客网 时间:2024/05/19 12:16
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include "test.h"
using namespace std;
//*********************************************************************************
//FUNCTION:
void print()
{
cout << "print is called!" << endl;
}
void fun()
{
cout << "fun is called!" << endl;
}
static void (* _malloc_alloc_oom_handle)() = NULL;
static void (* set_malloc_handle(void (*f)()))() //必须返回一个void _fun(void) 的函数指针, 或者根本不返回
{
void (* old)() = _malloc_alloc_oom_handle;
_malloc_alloc_oom_handle = f;
return (old);
}
//*********************************************************************************
//FUNCTION:
void putString(char *p)
{
if (NULL == p)
{
cout << "putString is called!" << endl;
return ;
}
cout << p << endl;
}
void hellow(char *p)
{
if (NULL == p)
{
cout << "hellow word!" << endl;
return ;
}
cout << p << endl;
}
static void (* _malloc_alloc_oom_handle_p)(char *p) = NULL;
static void (* set_malloc_handle(void (*f)(char *p)))(char *p)
{//这个函数的参数为一个函数指针(这个函数指针指一个参数为char*且返回值为空的函数);这个函数返回一个函数指针,这个函数指针指一个参数为char*且返回值为空的函数。
void (* old)(char *pp) = _malloc_alloc_oom_handle_p;
_malloc_alloc_oom_handle_p = f;
return (old);
}
//*********************************************************************************
//FUNCTION:
int add(int x, int y)
{
cout << "add is called!" << endl;
return x + y;
}
int sub(int x, int y)
{
cout << "sub is called!" << endl;
return x - y;
}
static int (* _malloc_alloc_oom_handle_int)(int x, int y) = NULL;
static int (* set_malloc_handle(int (*f)(int x, int y)))(int x, int y)
{
int (* old)(int xx, int yy) = _malloc_alloc_oom_handle_int;
_malloc_alloc_oom_handle_int = f;
return old; //这句话去掉也是可行的
//return 1; //报错
}
int main()
{
_malloc_alloc_oom_handle = print;
void (* old)() = set_malloc_handle(fun);
_malloc_alloc_oom_handle();
old();
_malloc_alloc_oom_handle_p = putString;
void (*oldp)(char *p) = set_malloc_handle(hellow);
_malloc_alloc_oom_handle_p("wyp wyp");
oldp(NULL);
_malloc_alloc_oom_handle_int = add;
int (* oldint)(int x, int y) = set_malloc_handle(sub);
int reslut;
reslut = _malloc_alloc_oom_handle_int(10 , 10);
cout << reslut << endl;
reslut = oldint(10, 10);
cout << reslut << endl;
system("pause");
return 0;
}
- 函数指针学习(还没明白具体含义难点)
- 指针基础(此文还没涉及指针作为函数形参)
- j2sdk环境变量设置还没明白
- PropertyGrid 用法,还没明白呢.
- 说实话还没明白这是什么,但是可以试试看
- 字符串组合算法--但是思路还没全部搞明白
- poj 2001 Shortest Prefixes 还不是很没明白,还得再理解理解
- WSAGetLastError()函数返回值的具体含义
- 查看还没加密的函数
- 还没开始学习呢--计划前奏
- 并发学习之:Keyed Events(没看懂,要调试下才能明白,伤心!)
- 还没
- centos学习 明白命令行、make编译、指针
- C语言种指针难点(1)
- 三个Oracle日期函数(十分有用)!!!!!!!!--我还没验证过
- C语言难点专题——函数指针
- RECT 没太明白
- 没明白 / 重点
- 解决:用户 'NT AUTHORITY\NETWORK SERVICE' 登录失败 ASP.NET程序
- oracle本月、上月、去年同月第一天最后一天
- 提问c# action<> func<> 这2个委托怎么用和理解
- MySQL 导入慢的解决方法
- PS里面如何批处理图片
- 函数指针学习(还没明白具体含义难点)
- C++浅拷贝和深拷贝的区别
- Windows 下OpenSSL 安装
- 利用word2vec对关键词进行聚类
- [python]-urllib2和urllib模块使用
- PL/SQL 连接远程数据库
- ORACLE分页查询SQL语法
- 费解了两天,,原来编译器自身的问题
- DSPIC随笔1-----------------------------------关于DSPIC单片机的指令周期