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;
}