Cpp_Primer--函数
来源:互联网 发布:大数据架构师要求 编辑:程序博客网 时间:2024/06/11 12:51
const 形参与实参-6.2.3
形参中的顶层const会被忽略(p.212),注意,这里的忽略并不是指无效,
而是指用实参初始化形参时,可以用非const的实参来初始化const形参
但是这里要注意的是声明中加const了与未加的会冲突,见代码
顶层const(top-level const) 表示对象本身是不可修改的
底层const(low-level const) 表示指针/引用所指向的对象是不可修改的(p.63)
const int ci = 42; // top-level constint i = ci;int *const p = &i; //top-level const*p = 0; //okvoid fcn(const int i){}void fcn(int i){} //error:redefines fcn(int)
执行需要输入的main函数-6.2.5
#include<iostream>using namespace std;//运行时使用cmd输入 E:\Cpp\Cpp_Primer\Debug\Cpp_Primer.exe -d -o ofile data9//其中 E:\Cpp\Cpp_Primer\Debug\Cpp_Primer.exe 是执行文件在系统中的路径...开学了要学Linux了...int main(int argc, char* arg[]){ string s; for (int i =1;i<argc;++i) { s += arg[i]; } cout << s << endl; return 0;}
函数重载与const_cast应用
#include<iostream>#include<string>using namespace std;const string &shorterString(const string &s1, const string &s2){ return s1.size() <= s2.size() ? s1 : s2;}string &shorterString(string &s1, string &s2){ auto &r = shorterString(const_cast<const string&> (s1), const_cast<const string&> (s2)); return const_cast<string&>(r);}int main(){ string s1 = "longer"; string s2 = "short"; string s3 = shorterString(s1, s2); cout << s3 << endl; const string constS1 = "const_longer"; const string constS2 = "const_short"; string S3 = shorterString(constS1, constS2); cout << S3 << endl; S3 = "dd"; cout << S3 << endl;}
默认初始形参初始值
这里为什么全局的wd被隐藏后,在调用函数并赋予初始值时仍为全局的wd?
#include<iostream>#include<string>using namespace std;int wd = 80;char def = ' ';int hz(){ return 10;}int screen(int a= hz(), int b= wd, char c= def){ cout << a << endl; cout << b << endl; cout << c << endl; return hz()+wd;}int window = screen();int main(){ def = '*'; int wd = 100; window = screen(); ::wd = 100; window = screen();}
重载与作用域-6.4.1
#include<iostream>using namespace std;string read();void print(const string &);void print(double);void fooBar(int ival){ bool read = false;//隐藏了外层read string s = read(); void print(int);//隐藏了外层所有的print print("Value:");//error! print(ival);//print(int) print(3.14);//print(int)}
把内联函数和constexpr函数放在头文件内-6.5.2
获取一些DEBUG信息-6.5.3
#include<iostream>using namespace std;void errorMessage(void){ cerr << "Error: " << __FILE__ << " : in function " << __func__ << " at line " << __LINE__ << endl << " Compiled on " << __DATE__ << " at " << __TIME__ << endl}int main(){ errorMessage();}
函数匹配-6.6
- 确定备选函数
- 寻找最佳匹配 对多形参的函数匹配若不同参数对于不同函数匹配等级相同或矛盾,则为二义性而拒绝请求
- 同一参数对应的两个函数为相同匹配等级
- a参数对应f1等级高,b参数对应f2等级高
- 匹配等级
- 精确匹配:
- 实参类型与形参相同
- 实参从数组类型或者函数类型转换成对应的指针类型
- 向实参添加顶层const或从实参中删除顶层const
- 通过const转换实现的匹配(见4.11.2)
- 通过类型提升实现的匹配(4.11.1)
- 通过算术类型转换(4.11.1)或者指针转换(4.11.2)实现的匹配
- 通过类类型转换实现的匹配(14.9)
Some example
- 精确匹配:
#include<iostream>using namespace std;void lookup(int&){ cout << __func__ << "int&";}void lookup(const int&){ cout << __func__ << "const int&";}const int a;int b;void manip(int, int){ cout << __func__ << "int int";}int main(){ lookup(a);// calls lookup(const int&) lookup(b);// calls lookup(int&) double dobj; manip('a', 'z');//Match through a promotion manip(55.4, dobj);//Arithmetic type conversion}
函数指针-6.7
一般例子:http://www.cnblogs.com/uniqueliu/archive/2011/07/27/2118619.html
#include <iostream>#include <vector>using namespace std;int f(int a, int b){ return a+b;}int main(){ vector<int(*)(int, int)>vec; vec.push_back(f); cout << vec[0](10, 5) << endl;;}
0 0
- Cpp_Primer--函数
- Cpp_Primer--Arrays
- CPP_Primer 笔记-ch1
- CPP_Primer CH6--抽象容器
- Cpp_Primer--运算符
- Cpp_Primer--类型转换
- Cpp_Primer--异常检测入门
- Cpp_Primer--Chapter8--IO库
- Cpp_Primer--学习的相关资源
- Cpp_Primer--using注意事项 与 Range-Based for
- Cpp_Primer--左值与右值,求值顺序
- 函数
- 函数
- 函数
- 函数
- 函数
- 函数
- 函数
- 机器学习算法比较
- 【Qt开发】事件循环与线程 二
- 中秋之夜 一个感人的flash动画小故事
- POJ2255----Tree Recovery
- Android与设计模式之Builder
- Cpp_Primer--函数
- uva 839 Not so Mobile 二叉树
- javaWeb中小问题,实体的DAO方法中定义实体对象问题
- MIT6.00 1x Lecture 2 - Core elements of programs 学习笔记
- JTAG的调试原理
- 推荐系统中所使用的混合技术介绍
- 给瀑布流加入模态对话框碰到的问题
- 跨越千年的RSA算法
- numpy的random