1,生成一个C++程序共有三个步骤。首先,代码在预处理器中运行,预处理器会识别代码中的元信息。其次,代码被编译或转换成计算机可识别的目标文件。最后,独立的目标文件被连接在一块儿变成一个应用程序。
2,名称空间
定义:namespace mycode { void foo(); }
实现:void mycode::foo() { ... }
使用方法:a, using namespace mycode; foo(); b, using mycode::foo; foo();
C++17中的名称空间嵌套:namespace MyLib::NewWorking::FTP { ... }
给名称空间起别名:namespace MyFTP = MyLib::NewWorking::FTP;
3,类型转换
float myFloat = 3.14f;
int i3 = static_cast<int>(myFloat);
4,强类型的enum class:
定义:enum class PieceType { King = 1, Queen, Rook = 10, Pawn };
使用:PieceType piece = PieceType::King;
枚举值不会自动转换为整数:if (PieceType::Queen == 2) { ... } // 这段代码不合法
能够这样改变枚举值类型(默认是整型):
enum class PieceType : unsigned long { King = 1, Queen, Rook = 10, Pawn };
5,if 语句的初始化器
if ( Employee employee = GetEmployee(); employee.salary > 1000 ) { ... }
switch 语句也有这种用法
6,数组与容器
array<int, 3> arr = { 1, 2, 3 };
vector<int> myArr = { 11, 22, 33 };
7,基于区间的for循环
std::array<int, 4> arr = { 1, 2, 3, 4 };
for ( int i : arr ) { std::cout << i << std::endl; }
8,初始化列表
利用初始化列表可轻松地编写能接收可变数量参数的函数
#include <initializer_list>
using namespace std;
int makeSum( initializer_list<int> lst )
{
int total = 0;
for ( int value : lst ) { total += value }
return total;
}
int c = makeSum( {1, 2, 3, 4, 5} );
9,空指针在布尔表达式中使用时会被转换为false,对null或未初始化的指针解除引用会致使不可肯定行为
10,动态分配数组
int arrSize = 8;
int* myVariableSizeArr = new int[arrSize];
指针变量仍在堆栈中,但动态建立的数组在堆中
11,C++11以前,常量NULL用于表示空指针,将NULL定义为常量0会致使一些问题,应使用nullptr;
12,智能指针
std::unique_ptr
auto employee = make_unique<Employee>();
auto employee = make_unique<Employee[]>(10); employee[0].salary = 1000;
std::shared_ptr
auto employee = make_shared<Employee>();
shared_ptr<Employee[]> employee(new Employee[10]);
13,引用
int x = 32;
int& xReference = x;
按引用传参的函数传递字面量是会编译错误
14,异常
#include <stdexcept>
double divideNumbers(double numerator, double denominator)
{
if ( denominator == 0 )
throw invalid_argument("Denominator cannot be 0.");
return numerator / denominator
}
try {
cout << divideNumbers(2.5, 0.5) << endl;
cout << divideNumbers(3.2, 0) << endl;
cout << divideNumbers(5.2, 1.2) << endl; // 第二句报错,此句不会执行
} catch (const invalid_argument& exception) {
cout << "Exception caught: " << exception.what() << endl;
}
15,类型推断
auto: auto去除了引用和const限定符,会创造副本!若是不须要副本,可以使用auto& 或 const auto&
decltype: 把表达式做为实参,计算出该表达式的类型,decltype不会除了引用和const限定符!int x = 123; decltype(x) y = 234;
16,类与对象,若是构造函数只是初始化数据成员而不作其余事,实际上没有必要使用构造函数,由于可在类定义中直接初始化数据成员
17,统一初始化
CircleStruct myCircle1 = { 10, 10, 2.5 };
CircleClass myCircle2 = { 10, 10, 2.5};
CircleStruct myCircle3 { 10, 10, 2.5 };
CircleClass myCircle4 { 10, 10, 2.5}; // 这四句同样效果
并不局限于结构体和类,例如:
int a = 3;
int b(3);
int c = {3};
int d{3}; // 这四句效果也同样
统一初始化还可用于将变量初始化为0,只须要指定对应的空大括号:int e{};
统一初始化还能够阻止C++隐式的窄化,如:
int x = 3.14; // 有些编译器只会发出警告甚至没有警告
int x = {3.14}; // 这样编译会报错
统一初始化还能够动态分配数组:int* pArr = new int[4]{1, 2, 3, 4};
18,直接列表初始化与复制列表初始化
从C++17开始:
// 复制列表初始化
auto a = {11};
auto b = {11, 22};
// 直接列表初始化
auto c {11};
auto d {11, 22}; // 报错,太多元素