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:转载请标注出处.本文不得作为商业活动.若有违本之,则本人不负法律责任.违法 ...
随机推荐
- Hive查看table在HDFS上的存储路径
hive>show databases;hive>use databasename;hive>show create table tablename; --查看table的存储路径h ...
- mysql 慢查询日志,灾难日志恢复,错误日志
灾难日志 记录了所有的DDL(Create.Drop和Alter)和DML(insert.update.delete_的语句,但不包括查询的语句 打开mysql.ini 找到Binary Loggin ...
- python标准库介绍——26 getopt 模块详解
==getopt 模块== ``getopt`` 模块包含用于抽出命令行选项和参数的函数, 它可以处理多种格式的选项. 如 [Example 2-23 #eg-2-23] 所示. 其中第 2 个参数指 ...
- [sh]ls -F一种非常有用的ls格式
ls -F一种非常有用的ls格式 tz/y/yupeng > ls -F#q# News/ doc/ images/ mbox ...
- UnityTestTools測试工具
由于工作关系,要了解Unity上的測试工具,该工具基于Nunit框架.通过查阅资料了解到在Unity5.3中做出了一些改变,自带的仅仅剩下单元測试工具,假设想用其它的工具比方断言.集成測试,就须要前往 ...
- lua字符串
本文内容基于版本:Lua 5.3.0 概述 Lua字符串中的合法字符可以是任何的1字节数据,这包括了C语言中表示字符串结束的'\0'字符,也就是说Lua字符串在内部将以带长度的内存块的形式存储,存储的 ...
- 关于Solr6.0中solrj使用简单例子
solr6.0的solrJ接口有部分变化,下面列出了简单的使用实例,有需要的朋友可以参考下. package com.ailk.solr6; import java.io.IOException; i ...
- vue2.0的常用功能简介
路由跳转 当我们想要实现点击链接跳转时,可以使用$router来进行跳转 语法如下: '}}) 这里path是要跳转的路径,query里面是路径跳转时要携带的参数,以对象的形式存在 2 获取路由参数 ...
- div设置contentEditable="true"作为文本编辑器,定位光标解决办法
function set_focus(el) { el = el[0]; // jquery 对象转dom对象 el.focus(); if ($.browser.msie) { ...
- 在WEB开发的时候导入各种jar包
使用eclipse导入很简单 右击你的project,选择properties,然后选择java build path,接着选择libraries,点击add external jars即可 如果你还 ...