nullptr
对于c中null = (void*)0,所以在为函数传参传入0时,无法清楚地分辨是int类型的0还是指的是空指针null
在C++11中清晰的将空指针变为了nullptr,0专指int型的数字0
override关键字
在子类中对父类的函数的覆写之后加上override关键字,编译器就可以检查是否出错,比如拼写是否有错和参数个数是否正确
final关键字
有两个用途,一个是组织从类继承,一个是阻止虚函数的覆写
#include <iostream>
using namespace std;class A final
{};class B : public A //错误,由于A类有final关键字,A类无法被继承
{};
int main()
{cout << "final key word." << endl;return 0;
}
#include <iostream>
using namespace std;class A //final
{public:virtual void show()final{};
};class B : public A
{public:virtual void show()override{}; //错误,因为父类中的虚函数带有final关键字,不能被覆写
};
int main()
{cout << "final key word." << endl;return 0;
}
default关键字
令函数等于default,就是使用系统默认提供的函数(如果有的话)
#include <iostream>
using namespace std;class A
{public:A() = default; //这里相当于默认提供的无参默认构造函数A(){}
// ~A(){}
// A(const A &another){}
// A operator=(const A &another){}A(int a){}
};int main()
{A a;return 0;
}
delete关键字
能够让程序员显式的禁用某个函数,在函数声明后加上“= delete”,就可以将此函数禁用
#include <iostream>
using namespace std;class A
{public:static A* get_A(){if(p == nullptr)p = new A;return p;}A() = default;~A() = delete;A(const A &another){}A operator=(const A &another){}private:static A* p;
};
A* A::p = nullptr;int main()
{A* ap = A::get_A();delete ap; //illegal,这里不能delete,因为在A类中显式的禁用了A的析构函数return 0;
}
原生字符串raw string
为了解决字符串中一些字符必须经过转义才能输出的问题
#include <iostream>
#include <string.h>
using namespace std;int main()
{//原本字符串为C:\dell\UpdatePackage\log string str1 = "C:\\dell\\UpdatePackage\\log";cout << str1 << endl;string str2 = R"(C:\dell\UpdatePackage\log)"; //将str2定义为原生字符串 cout << str2 << endl;
}
auto-for循环(范围for循环)
#include <iostream>
using namespace std;int main()
{int p[] = {1,2,3,4,5,6,7,8,9,10};for(auto &i : p){cout << i << endl;}
// const char *q = "china";//这里的指针指向的是字符串字面量,不可改变,要加const
// for(const auto &i : q)//illegal,字符指针这种格式不带有begin、end,
// //但是范围for循环底层是用begin、end来实现的
// {
// cout << i << endl;
// }char m[] = "china"; //这样以数组的形式是可以使用范围for循环的for(auto &i : m){cout << i << endl;}char n[] = {'c','h','i','n','a'};for(auto &i : n){cout << i << endl;}return 0;
}