C++ Primer学习笔记(三) C++中函数是一种类型!!!
C++中函数是一种类型!C++中函数是一种类型!C++中函数是一种类型!
函数名就是变量!函数名就是变量!函数名就是变量!
(---20160618最新消息,函数名不是变量名。。。囧)
(---20160714最新消息,C++没有函数类型。。。囧)
(---但是,我认为把它们当成类型和变量更容易理解!!!)
重要的事情要说三遍。。。
#include <iostream>
#include <string> using namespace std; //test const
int main(){
//------------测试非引用------------
int no=;
const int no2=no; //OK
int no3=no2; //OK!
//上面得出结论:非引用类型的赋值无所谓const //------------测试引用------------
int &noref=no;
const int &noref1=no; // int &no2ref=no2;
const int &no2ref1=no2; int &no3ref=no3;
const int &no3ref1=no3;
// 上面得出结论:const引用可以指向const及非const。但非const引用只能指向非const。 //------------测试指针------------
int *pno=&no;
const int *pno_1=&no;//指向const的指针,可以指向非const // int *pno2=&no2;//指向非const的指针,只能指向非const
const int *pno2_1=&no2; int *pno3=&no3;
const int *pno3_1=&no3;
// 上面得出结论:见备注 return ;
}
可基于函数的引用形参是指向 const 对象还是指向非 const 对象, 实现函数重载。将引用形参定义为 const 来重载函数是合法的,因为编译器可以根据实参是否为 const 确定调用哪一个函数。
typedef bool (*cmpFcn) (const string &, const string &);
所以,可以使用函数名对函数指针进行初始化或赋值。
cmpFcn pf1=; //ok
cmpFcn pf2=lengthCompare; //ok
pf1=lengthCompare; //ok
pf2=pf1; //ok
cmpFcn pf1=lengthCompare;
cmpFcn pf2=&lengthCompare;
lengthCompare("hi", "bye"); //直接调用函数
pf("hi", "bye"); //隐式解引用!
(*pf)("hi", "bye"); //显式解引用!
void func(const string &, const string &, bool(const string &, const string &)); //隐式
void func(const string &, const string &, bool (*)(const string &, const string &)); //显式
int (*ff(int))(int*,int); //QNMD
其中:ff(int) 是函数,返回 int (*)(int*,int),也就是返回函数指针。
#include <iostream>
#include <string>
using namespace std; int strCmp(const string&, const string&);
int xxx(const string&, const string&); //函数指针
int main(){
string str1="hehe";
string str2="abc"; cout<<"strCmp(str1, str2):"<<strCmp(str1, str2)<<endl; //函数指针
int (*ext)(const string&,const string&); //(*strCmp)的括号是必须的。如果声明:int *strCmp(const string&, const string&) typedef int (*fp)(const string&, const string&);//类似声明
fp p0=;
cout<<"p0:"<<p0<<endl;
fp p1=strCmp;
fp p2=p1;
p0=p2; cout<<"p0(\"---\", \"xxxxxxx\")"<<p0("---", "xxxxxxx")<<endl;
cout<<"p1(str2, str1):"<<p1(str2, str1)<<endl; //why?
cout<<"p2(str2, str1):"<<p2(str2, str1)<<endl; //why? fp p3=xxx;
cout<<"p3:"<<p3<<endl;//why 1?
cout<<"p3(\"a\", \"b\"):"<<p3("a", "b")<<endl; ext =xxx;
cout<<"ext:"<<ext<<endl;//why 1?
cout<<"ext(str1, str2):"<<ext(str1, str2)<<endl; //结论:type (*pf)(parameter list)就已经定义了一个函数指针pf。
//typedef可以定义函数指针的类型,而非函数指针。该类型可以定义指针。
//0函数指针输出0;其他则输出1。
//通过函数指针可以直接调用函数:只要后面跟上实参列表即可!函数指针会隐式的解引用--当然也可以显式的解引用! return ;
} int strCmp(const string &str1, const string &str2){
return str1.size()-str2.size();
} int xxx(const string &str1, const string &str2){
return ;
}
typedef int func(p.l);//这里的func是函数类型,不是函数指针!所有int xxx(p.l)形式的函数!
void f1(func); //ok 函数类型可以作为形参
func f2(int); //error 函数类型不能作为返回值!
func *f3(int); //ok 函数类型指针可以作为返回值(其实就是函数指针)
C++ Primer学习笔记(三) C++中函数是一种类型!!!的更多相关文章
- Struts2学习笔记(三):result配置的各项视图转发类型
Struts 1: <action path="/user" type="org.sunny.user.action.UserAction" ...> ...
- MySQL学习笔记(三):常用函数
一:字符串函数 需要注意的几个细节: 1.cancat中有一个字符串为null,则结果为null. 2.left(str,x) 和 right(str,x)中x为null,则不返回任何字符串,不是nu ...
- angular学习笔记(三)-视图绑定数据的两种方式
绑定数据有两种方式: <!DOCTYPE html> <html ng-app> <head> <title>2.2显示文本</title> ...
- C++ Primer学习笔记(二)
题外话:一工作起来就没有大段的时间学习了,如何充分利用碎片时间是个好问题. 接 C++ Primer学习笔记(一) 27.与 vector 类型相比,数组的显著缺陷在于:数组的长度是固定的,无法 ...
- Typescript 学习笔记三:函数
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
- ES6学习笔记<三> 生成器函数与yield
为什么要把这个内容拿出来单独做一篇学习笔记? 生成器函数比较重要,相对不是很容易理解,单独做一篇笔记详细聊一聊生成器函数. 标题为什么是生成器函数与yield? 生成器函数类似其他服务器端语音中的接口 ...
- MYSQL学习笔记三:日期和时间函数
MYSQL学习笔记三:日期和时间函数 1. 获取当前日期的函数和获取当前时间的函数 /*获取当前日期的函数和获取当前时间的函数.将日期以'YYYY-MM-DD'或者'YYYYMMDD'格式返回 */ ...
- [Firefly引擎][学习笔记三][已完结]所需模块封装
原地址:http://www.9miao.com/question-15-54671.html 学习笔记一传送门学习笔记二传送门 学习笔记三导读: 笔记三主要就是各个模块的封装了,这里贴 ...
- 学习笔记(三)--->《Java 8编程官方参考教程(第9版).pdf》:第十章到十二章学习笔记
回到顶部 注:本文声明事项. 本博文整理者:刘军 本博文出自于: <Java8 编程官方参考教程>一书 声明:1:转载请标注出处.本文不得作为商业活动.若有违本之,则本人不负法律责任.违法 ...
随机推荐
- 关于apache服务器加载so的报错
早上突然发现我的虚拟机上的WEB应用访问不了了,后台检查httpd服务,无法启动,出现一行提示: ①starting httpd: httpd: Syntax error on line 163 of ...
- 使用和学习 ES2015
调试网站 http://babeljs.io/repl/ 扩展阅读: # export.exports.modules.exports 和 require .import 的一些常用方法和套路 htt ...
- python标准库介绍——28 sha 模块详解
==sha 模块== ``sha`` 模块提供了计算信息摘要(密文)的另种方法, 如 [Example 2-39 #eg-2-39] 所示. 它与 ``md5`` 模块类似, 但生成的是 160 位签 ...
- Spring Cloud Netflix概览和架构设计
Spring Cloud简介 Spring Cloud是基于Spring Boot的一整套实现微服务的框架.他提供了微服务开发所需的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁.决策 ...
- php分页函数示例代码
分享一例php分页函数代码,用此函数实现分页代码很不错. 代码,php分页函数. <?php /* * Created on 2011-07-28 * Author : LKK , http:/ ...
- 记一次Animator状态快速切换问题的解决
事情是这样的,我尝试在一帧内多次切换一些状态(当前状态为Idle的情况下): public Animator animator; void OnEnable() { animator.CrossFad ...
- kvm最小磁盘大于等于5G
上图的实验为4G(磁盘的大小) 后来磁盘大小增加到5G后,成功安装! [root@bass virhost]# virt-install --name 22cache --ram=512 --arch ...
- Mysql修改时间的年月日,时分秒保持不变语句
比如时间为 “2015-01-05 14:32:21” 修改为“2015-01-06 14:32:21” 修改内容为修改yyyy-mm-dd为对应的日期,时间不变. HOUR,TIME(StatusU ...
- python+xpath+requests爬取维基百科历史上的今天
import requests import urllib.parse import datetime from lxml import etree fhout = open("result ...
- LeetCode: Permutations II 解题报告
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...