【C++】函数和指针
最近在看C++ primer plus,感觉函数与指针这一章难点比较多,记写笔记,加强理解.
From C++ Primer Plus: Chapter 7 Function:C++ Programming Modules
1. 如何声明函数指针?
和函数原型类似: 需要声明指针指向函数的返回值和参数列表
double pam(int); //参数为int 类型,返回值为double 类型的函数
double (*pf);(int) //指向参数为int类型,返回值为double 类型的指针
pf = pam; //函数名代表了函数的地址 double x = pam(); //函数名调用
double x = (*pf)(); //指针调用
double x = pf(); //C++也允许将指针名当作函数名使用
2. C++ 11 自动类型推断
const double * f1(const double *, int); const double * (*p1)(const double *, int); //p1 poitns to f1
auto p2 = f1; //C++11 automatic type deduction,p2 points to f1 as well
3. 将指针名当作函数名使用
//前面函数为double *类型,cout第一部分返回double指针,第二部分返回double指针指向的值
cout<<(*p1)(av,)<<":"<<*(*p1)(av,)<<endl;
//和上面的cout一样只不过是使用函数指针名来调用函数
cout<<p2(av,)<<":"<<*p2(av,)<<endl;
4. 函数指针数组
const double *(*pa[]) (const double *,int) = {f1,f2,f3}; //创建函数指针数组
//通过指针调用函数,得到返回的指针
const double *px = pa[](av,); //call by pointer as if it were a function name
const double *py = (*pa[])(av,); //正常调用
//得到函数返回指针指向的值
double x = *pa[](av,);
double x = *(*pa[])(av,);
5. 指向指针数组的指针
指针数组和数组指针的区别
*pd[] //an array of 3 pointers
(*pd)[] //a pointer to an array of three elements
指向数组的指针
auto pc = &pa; //&pa是整个数组的地址, pa是数组第一个元素首地址 const double * (*(*pd)[])(const double *, int ) = &pa; //和第一个等价 **&pa = *pa = pa[]
代码:
//arfupt.cpp -- an array of function pointers
#include<iostream>
//various notations,same signatures
const double *f1(const double ar[],int n);
const double *f2(const double [],int);
const double *f3(const double *,int); int main()
{
using namespace std;
double av[] = {1112.3,1542.6,2227.9}; //pointer to a function const double *(*p1)(const double *,int) = f1;
auto p2 = f2;//C++ 11 utomatic type deduction
//pre-C++11 can use the following code instead
//const double *(*p2)(const double *,int) = f2;
cout<<"Using pointers to functions:\n";
cout<<"Address Value\n";
cout<<(*p1)(av,)<<":"<<*(*p1)(av,)<<endl;
cout<<p2(av,)<<":"<<*p2(av,)<<endl; //pa an array of pointers
//auto doesn't work with list initialization
const double *(*pa[])(const double *,int) = {f1,f2,f3};
//pb a pointer to first element of pa
auto pb = pa;
// pre-C++11 can use the following code instead
// const double *(**pb)(const double *, int) = pa;
cout<<"\nUsing an array of pointers to functions:\n";
cout<<"Address Value\n";
for(int i = ;i < ; i++)
cout<<pa[i](av,)<<":"<<*pa[i](av,)<<endl;
cout<<"\nUsing a pointer to a pointer to a function:\n";
cout<<"Address Value\n";
for(int i = ;i < ; i++)
cout<<pb[i](av,)<<":"<<*pb[i](av,)<<endl; //what about a pointer to an array of function pointers
cout<<"\nUsing pointers to an array of pointers:\n";
cout<<"Address Value\n";
//easy way to declare pc
auto pc = &pa;
// pre-C++11 can use the following code instead
// const double *(*(*pc)[3])(const double *, int) = &pa;
cout<<(*pc)[](av,)<<":"<<*(*pc)[](av,)<<endl;
//hard way to declare pd
const double *(*(*pd)[])(const double *,int) = &pa;
//store return value in pdb
const double *pdb = (*pd)[](av,);
cout<<pdb<<":"<<*pdb<<endl;
//alternative notation
cout<<(*(pd)[])(av,)<<":"<<*(*(*pd)[])(av,)<<endl;
} const double * f1(const double * ar, int n)
{
return ar;
}
const double * f2(const double ar[], int n)
{
return ar+;
}
const double * f3(const double ar[], int n)
{
return ar+;
}
【C++】函数和指针的更多相关文章
- C语言中 指向函数的指针 简介
引子:在学习CPrimerPlus的第十四章的14.13节中,遇到了如下三行文字,是有关指向函数的指针的,把我搞晕了. char * fump(); //返回指向char的指针的函数 char (* ...
- 12-返回指针的函数&&指向函数的指针
前言 接下来我只讲指针的最常见用法,比如这一章的内容----返回指针的函数 与 指向函数的指针 一.返回指针的函数 指针也是C语言中的一种数据类型,因此一个函数的返回值肯定可以是指针类型的. 返回 ...
- 《征服 C 指针》摘录4:函数 与 指针
一.指向函数的指针 函数名可以在表达式中被解读成“指向函数的指针”,因此,正如代码清单 2-2 的实验那样,写成 func 就可以取得指向函数的指针. “指向函数的指针”本质上也是指针(地址),所以可 ...
- 【C语言】14-返回指针的函数与指向函数的指针
前言 前面我们花了接近3个章节学习指针,应该都感受到指针的强大了吧.指针可以根据地址直接操作内存中的数据,使用得当的话,不仅能使代码量变少,还能优化内存管理.提升程序性能.关于指针的内容还非常多,比如 ...
- 指向函数的指针与iOS-Block相关知识
指向函数的指针与iOS-Block相关知识 一. 函数指针的定义和调用: 关于函数指针的知识详细可参考:http://www.cnblogs.com/mjios/archive/2013/03/19/ ...
- C++ 必知必会:条款16 指向成员函数的指针并非指针
这一点与指向成员的指针类似,其实现可能更加复杂,因为成员函数同时还存在虚拟函数,需要动态绑定执行动作.当然这种属性是属于函数本身的,此处表达的是指针不涉及函数的属性问题. 1: class shape ...
- 指向函数的指针数组(C++)
我们能够创建一个指向函数的指针数组.为了选择一个函数,只需要使用数组的下标,然后间接引用这个指针.这种方式支持表格式驱动码的概念:可以根据状态变量去选择被执行函数,而不用条件语句或case语句.这种设 ...
- 【学习笔记】【C语言】指向函数的指针
每个函数都有自己的内存地址,指针保存了函数的地址后就能指向函数了. #include <stdio.h> double haha(double d, char *s, int a) { } ...
- C++学习笔记:指向函数的指针
#include <stdio.h> int sum(int a, int b) { return a+b; } int minus(int a, int b) { return a-b; ...
- c语言学习之基础知识点介绍(十五):函数的指针
一.函数的指针的介绍 /* 函数指针: 函数的指针,本质上一个指针 指向函数的指针,就是一个函数指针. 回忆:我们写的源代码编译成二进制的指令集,一串交给CPU执行的指令 先存在内存里面,然后CPU读 ...
随机推荐
- 有效解决ajax传中文时,乱码的情况,php处理接收到的值
在抽奖环节时,需把获奖名单通过ajax的post方式传输给php后台进行储存,但是php接收到的值确是乱码.在百度之后并没有找到合适的解决方法. 则使用js的encodeURI函数可以有效解决,但不知 ...
- wordpress 拾遗
wordpress 拾遗 运行环境 php mySQL Apache 集成开发环境 Appserv xampp phpstudy 文章和页面的区别 文章是发布网站主要内容的地方,比如博客的文章,商城的 ...
- GRUB 引导流程
GRUB(bootloader)引导流程: GRUB,GRand Unified Bootlader ,是一个来自GUN项目的多操作系统启动程序.GRUB是多启动规范的实现,它允许用户可以在计算机内 ...
- spring-boot-maven-plugin 插件的作用(转)
OM 文件中添加了“org.springframework.boot:spring-boot-maven-plugin”插件.在添加了该插件之后,当运行“mvn package”进行打包时,会打包成一 ...
- HDU 2512
水题 #include <iostream> #include <cstdio> #include <algorithm> #define LL __int64 # ...
- android 读取xml
在有些应用中,有一点小数据.直接存储在XML就是.实现较为简单, 1.xml文件放入asset目录.结构如: <?xml version="1.0" encoding=&qu ...
- stl之hash_map
- java-面向对象(二)
这几天正在看HeadFirst的时候,突然认为面向对象的几个特点,有点理解模糊不清.所以在这再次回想一下,加深印象. 上篇博客(http://blog.csdn.net/u010539352/arti ...
- UVA - 10689 Yet another Number Sequence 矩阵快速幂
Yet another Number Sequence Let’s define another number sequence, given by the foll ...
- android 极细线
最后找到一个还算好用的方法:伪类 + transform 原理是把原先元素的 border 去掉,然后利用:before或者:after重做 border ,并 transform 的 scale 缩 ...