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 const
int i = ci;
int *const p = &i; //top-level const
*p = 0; //ok
void 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;;
}