一.缺省参数
1.全缺省参数
#include<iostream>
using namespace std;
void func1(int a = 10)
{
cout << a << endl;
}
int main()
{
func1();//10
}
#include<iostream>
using namespace std;
void func1(int a = 10,int b=20,int c=30)
{
cout << a <<' '<<b<<' '<<c<< endl;
}
int main()
{
func1();//10 20 30
}
2.半缺省参数
从右往左缺省(若从左往右缺省,则无法确定后面未缺省的值)
缺省参数声明和定义不能同时给,若存在声明,则只给声明缺省
缺省:不传值也能输出值
#include<iostream>
using namespace std;
void func1(int a=10 ,int b=20,int c)
{
cout << a <<' '<<b<<' '<<c<< endl;
}
int main()
{
func1(100,200);//报错:error C2548: “func1”: 缺少形参 3 的默认实参
}
#include<iostream>
using namespace std;
void func1(int a, int b = 20, int c = 30);
void func1(int a,int b,int c)
{
cout << a <<' '<<b<<' '<<c<< endl;
}
int main()
{
func1(100,200);//100 200 30
}
#include<iostream>
using namespace std;
void func1(int a, int b=20 , int c=30 );
void func1(int a,int b=40,int c=50)
{
cout << a <<' '<<b<<' '<<c<< endl;
}
int main()
{
func1(100,200);//报错:error C2572: “func1”: 重定义默认参数 : 参数 1
//报错:error C2572: “func1”: 重定义默认参数 : 参数 2
}
#include<iostream>
using namespace std;
void func1(int a, int b , int c );
void func1(int a,int b=40,int c=50)
{
cout << a <<' '<<b<<' '<<c<< endl;
}
int main()
{
func1(100,200);//100 200 50
}
#include<iostream>
using namespace std;
void func1(int a ,int b=20,int c=30)
{
cout << a <<' '<<b<<' '<<c<< endl;
}
int main()
{
func1(100,200);//100 200 30
}
二.函数重载
同一作用域函数名相同且形参列表(参数个数 或 类型 或 类型顺序)不同
返回值不同不能说明函数重载
函数重载只有c++支持,c不支持
#include<iostream>
using namespace std;
void f(int a)
{
cout << a << endl;
}
void f(int a, int b)
{
cout << a << ' ' << b << endl;
}
int main()
{
f(1);//1
f(20, 30);//20 30
}
//参数个数不同
#include<iostream>
using namespace std;
void f(int a)
{
cout << a << endl;
}
void f(double a)
{
cout << a << endl;
}
int main()
{
f(1);
f(20.2);
}
//参数类型不同
#include<iostream>
using namespace std;
void f(double b,int a)
{
cout << '1' << endl;
}
void f(int a,double b)
{
cout << '2' << endl;
}
int main()
{
f(1.1,2);//1
f(10,20.2);//2
}
//类型顺序不同
三.引用
取别名
定义时必须初始化
#include<iostream>
using namespace std;
int main()
{
int& b;//报错:error C2530: “b”: 必须初始化引用
}
一个变量多个引用
#include<iostream>
using namespace std;
int main()
{
int a = 0;
int& b = a;
int& c = a;
int& d = b;
}
引用权限放大缩小
权限放大报错,权限缩小和平移则运行成功
#include<iostream>
using namespace std;
int main()
{
const int a = 0;
int& b = a;//报错:error C2440: “初始化”: 无法从“const int”转换为“int &”
}
//权限放大
#include<iostream>
using namespace std;
int main()
{
int a = 10;
const int& b = a;
cout << b << endl;//10
}
//权限缩小
#include<iostream>
using namespace std;
int main()
{
const int a = 10;
const int& b = a;
cout << b << endl;//10
}
//权限平移
引用可影响被引用值
#include<iostream>
using namespace std;
int main()
{
int a = 10;
const int& b = a;
int& c = a;
c++;
cout << b << endl;//11
cout << a << endl;//11
cout << c << endl;//11
}
传值传参耗费时间,引用传参效率更高
类型转换--------产生临时变量,具有常性(由const修饰)
#include<iostream>
using namespace std;
int main()
{
double d = 12.34;
int i = d;
cout << i << endl;//12
}
#include<iostream>
using namespace std;
int main()
{
int x = 2, y = 3;
const int& t = x + y;
cout << t << endl;//5
}
#include<iostream>
using namespace std;
int main()
{
int x = 2, y = 3;
int& t = x + y;
cout << t << endl;//报错:error C2440: “初始化”: 无法从“int”转换为“int &”
}
#include<iostream>
using namespace std;
int main()
{
double d = 12.34;
int i = d;
int& r = d;
cout << r << endl;//报错:error C2440: “初始化”: 无法从“double”转换为“int &”
}
#include<iostream>
using namespace std;
int main()
{
double d = 12.34;
int i = d;
const int& r = d;
cout << r << endl;//12
}
取别名(语法上不开辟空间)
指针(语法上开辟空间)
没有NULL的引用
#include<iostream>
using namespace std;
int main()
{
int* ptr = NULL;
int& r = *ptr;//这时未解引用,存的是ptr的地址,不会报错
cout << r << endl;//此时系统崩溃
}
引用为引用类型的大小,但指针为地址空间所占字节个数
#include<iostream>
using namespace std;
int main()
{
int a = 10;
int& b = a;
cout << sizeof(a) << endl;//4
cout << sizeof(int&) << endl;//4
cout << sizeof(b) << endl;//4
}
有多级指针,没有多级引用
#include<iostream>
using namespace std;
int main()
{
int a = 10;
int b = 20;
int c = 30;
int& b = a;
int& b = c;//报错:error C2040: “b”:“int &”与“int”的间接寻址级别不同
}
引用自加即引用的实体增加
1
,指针自加即指针向后偏移一个类型的大小
(阿里巴巴2015笔试题) 引用传值,指针地址(X)
引用表面好像是传值,其
本质也是传地址,只是这个工作有编译器来做,所以错误
四.内联(inline)
在调用内联函数的地方展开,且没有函数调用建立栈帧,内联函数程序运行效率提高,缺陷是使目标文件变大
内联说明只是向编译器发出一个请求,编译器可以选择忽略这个请求(例如:函数规模大/递归/频繁调用)
inline
不建议声明和定义分离,分离会导致链接错误。因为
inline
被展开,就
没有函数地址
了,
链接就会找不到
宏的优缺点
五.auto
可以根据右边初始化判断类型
#include<iostream>
using namespace std;
int main()
{
auto x = 10;
auto y = 20.1;
std::cout << typeid(x).name() << std::endl; //int
std::cout << typeid(y).name() << std::endl; //double
}
//typeid().name 推测auto修饰变量类型
能够替代较长类型的定义,简化代码
auto和auto*没有区别
#include<iostream>
using namespace std;
int main()
{
int a = 10;
auto b = &a;
auto* c = &a;
auto& d = a;
cout << a << endl;//10
cout << b << endl;//006FFA4C
cout << c << endl;//006FFA4C
cout << d << endl;//10
}
auto不能作为函数参数(无法推导变量类型),不能声明数组
六.c++11 for循环
自动取array数组中,赋值给e
自动++,自动判断结束
#include<iostream>
using namespace std;
int main()
{
int array[] = { 1, 2, 3, 4, 5 };
for (auto& e : array)
e *= 2;
for (auto e : array)
cout << e << " ";//2 4 6 8 10
return 0;
}
auto e:array
范
围内用于迭代的变量
:
被迭代的范围
七.nullptr
没有对应的头文件
由于c++中定义的宏 #define NULL 0
故:
#include<iostream>
using namespace std;
void f(int)
{
cout << "f(int)" << endl;
}
void f(int*)
{
cout << "f(int*)" << endl;
}
int main()
{
f(0);//f(int)
f(NULL);//f(int)
f((int*)NULL);//f(int*)
return 0;
}
#include<iostream>
using namespace std;
int main()
{
cout << sizeof(nullptr) << endl;//4
cout << sizeof((void*)0) << endl;//4
}