1. new,delete的局部重載:ios
#include <iostream> using namespace std; int objs = 0; class myclass { public: myclass() { //objs++; cout << "create" << endl; } ~myclass() { //objs--; cout << "delete" << endl; } //operator重載,針對new從新做出一種解釋,只針對當前類 static void * operator new(size_t size) { objs++; cout << "new-call" << endl; myclass *p = ::new myclass; //全局new return p; } static void operator delete(void *p) { objs--; cout << "delete-call" << endl; ::delete p; } }; //功能1:沒法在堆上被建立的類 void main() { myclass *p1 = new myclass; myclass *p2 = new myclass; myclass *p3 = new myclass; delete p1; int *p = new int(5); //此時new重載對於這一句無效 cout << objs << endl; cin.get(); }
#include <iostream> using namespace std; int objs = 0; void *g_p = nullptr; class myclass { public: myclass() { //objs++; cout << "create" << endl; } ~myclass() { //objs--; cout << "delete" << endl; } //operator重載,針對new從新做出一種解釋,只針對當前類 static void * operator new(size_t size) { if (g_p==nullptr) { objs++; cout << "new-call" << endl; myclass *p = ::new myclass; //全局new g_p = p; //單例,堆上建立對象只有一個 return p; } else { return g_p; } } static void operator delete(void *p) { if (g_p!=nullptr) { objs--; cout << "delete-call" << endl; ::delete p; g_p = nullptr; } } }; //功能1:沒法在堆上被建立的類 //功能2:實現統計分配內存,釋放內存的次數 //實現單例設計模式,實現避免反覆delete出錯 //new delete在內部,只針對當前類,int double 無影響 void main() { myclass *p1 = new myclass; myclass *p2 = new myclass; delete p1; delete p1; //規避了兩次delete的錯誤 cin.get(); }
2. 全局new,delete重載:chrome
#include <iostream> using namespace std; //全局內存管理,統計釋放內存,分配內存 //new new [] delete delete [] //分配內存優先於構造 //析構優先於釋放內存 void * operator new(size_t size) { cout << "g_new call" << endl; void *p = malloc(size); //全局的new,只能使用malloc return p; } void * operator new [](size_t size) { cout << "g_new [] call" << endl; cout << size << endl; return operator new(size); //每一個元素調用一次new } void operator delete(void *p) { cout << "g_delete call" << endl; free(p); } void operator delete [](void *p) { cout << "g_delete [] call" << endl; free(p); } class myclass { public: myclass() { cout << "create call" << endl; } ~myclass() { cout << "delete call" << endl; } }; void main() { int *p1 = new int(5); delete p1; myclass *p2 = new myclass; delete p2; myclass *px = new myclass[10]; delete[]px; cin.get(); }
3. 綁定類成員函數:設計模式
#include <iostream> #include <functional> using namespace std; using namespace std::placeholders;//站位 struct MyStruct { void add1(int a) { cout << a << endl; } void add2(int a, int b) { cout << a << b << endl; } void add3(int a, int b, int c) { cout << a << b << c << endl; } }; void main() { MyStruct my1; //my1.add(10); //綁定包裝器,包裝類成員函數,用於使用 auto fun1 = bind(&MyStruct::add1, &my1, _1);//有1個參數 函數名、對象地址、參數 fun1(10); auto fun2 = bind(&MyStruct::add2, &my1, _1,_2);//有2個參數 fun2(100,200); auto fun3 = bind(&MyStruct::add3, &my1, _1,_2,_3);//有3個參數 fun3(1000,2000,3000); cin.get(); }
4. 綁定lambda表達式以及仿函數:數組
#include <iostream> #include <functional> using namespace std; using namespace std::placeholders; int add(int a, int b,int c) { return a + b+c; } struct MyStruct { int operator()(int a,int b) //仿函數 { return a + b; } }; void main() { auto fun1 = bind(add, 10,12, _1);//適配器模式 cout << fun1(1000) << endl; //1022 auto fun2 = bind([](int a, int b)->int {return a + b; }, 100, _1); cout << fun2(123) << endl; //223 MyStruct my1; cout << my1(1, 2) << endl; //3 auto fun3 = bind(my1, 100, _1); //綁定 cout << fun3(100) << endl; //200 cin.get(); }
5. 靜態斷言:函數
#include <iostream> #include <cassert> using namespace std; int divv(int a, int b) { assert(b != 0); //斷言 return a / b; } void main() { cout << divv(1, 0) << endl; cin.get(); }
#include <iostream> #include <cassert> using namespace std; static_assert(sizeof(void *) >= 8, "environment is not 64!"); void main() { cin.get(); }
6. 內聯函數:優化
#include <iostream> #include <cstdlib> using namespace std; #define f(x) x*x*x //C語言內聯,C++要求類型嚴格匹配 inline int get(int x) //C++的內聯函數 { return x*x*x; } //提升程序運行速度 //inline 只是對於編譯器的建議 //通常狀況下,咱們對內聯函數作以下的限制: //(1)不能有遞歸; //(2)不能包含靜態數據; //(3)不能包含循環; //(4)不能包含switch和goto語句; //(5)不能包含數組。 //若一個內聯函數定義不知足以上限制,則編譯系統把它當作普通函數對待 template<class T> inline T go(T t) { return t*t; } void main() { get(10); go(5); //優化爲內聯函數 auto fun = []() {}; //lambda表達式實際上也是內聯函數 cin.get(); }
7. CPP處理轉義字符:spa
#include <iostream> #include <string> #include <cstdlib> using namespace std; void main() { //string str("\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\""); string str(R"("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")"); // R"(......)" system(str.c_str()); cin.get(); }