delete 和 default 在 C++ 中用于显式控制默认函数的生成情况,
我们需要知道 C++ 编译器到底干了些什么
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| #define _CRT_SECURE_NO_WARNINGS
#include <iostream> #include <string> #include <vector> #include <map>
class X { public: X() = default; X(int i) { a = i; }
private: int a; };
X obj;
class X1 { public: int f() = default; X1(int, int) = default; X1(int = 1) = default; };
class X2 { public: X2() = default; X2(const X&); X2& operator = (const X&); ~X2() = default; };
X2::X2(const X&) = default; X2& X2::operator= (const X2&) = default;
class X3 { public: X3(); X3(const X3&) = delete; X3& operator = (const X3 &) = delete; };
class X4 { public: X4(double) {
}
X4(int) = delete; };
class X5 { public: void *operator new(size_t) = delete; void *operator new[](size_t) = delete; };
void mytest() { X4 obj1; X4 obj2=obj1;
X4 obj3; obj3=obj1;
X5 *pa = new X5; X5 *pb = new X5[10];
return; }
int main() { mytest();
system("pause"); return 0; }
|