Transferred from: Lsgxeva
#define_crt_secure_no_warnings#include<iostream>#include<string>#include<vector>#include<map>//c++11 class default function control: "=default" and "=delete" functions/*C + + classes have four special member functions, namely: Default constructors, destructors, copy constructors, and copy assignment operators. The special member functions of these classes are responsible for creating, initializing, destroying, or copying the objects of the class. If a programmer does not explicitly define a special member function for a class and needs to use that special member function, the compiler implicitly generates a default special member function for the class. *///The c++11 Standard introduces a new feature: the "=default" function. The programmer simply adds "=default;" After the function declaration, and the function can be declared as a "=default" function, and the compiler will automatically generate the function body for the explicitly declared "=default" function. classx{ Public: X ()=default;//This function obtains higher code efficiency than the user-defined default constructorXinti) {a=i; }Private: intA;}; X obj;//The "=default" function attribute applies only to special member functions of the class, and the special member function has no default parameters. classx1{ Public: intF () =default;//err, function f () Special member function of non-class XX1 (int,int) =default;//err, constructor X1 (int, int) non-X special member functionX1 (int=1) =default;//Err, default constructor X1 (int=1) contains default parameters};//the "=default" function can be defined either in the class body (inline) or outside the class body (out-of-line). classx2{ Public: X2 ()=default;//Inline defaulted default constructorX2 (Constx&); X2&operator= (Constx&); ~X2 () =default;//Inline defaulted destructor}; X2::X2 (Constx&) =default;//out-of-line defaulted copy constructorx2& X2::operator= (Constx2&) =default;//out-of-line defaulted copy assignment operator//to enable programmers to explicitly disable a function, the C++11 standard introduces a new feature: the "=delete" function. The programmer simply "=delete;" After the function declaration, the function can be disabled. classx3{ Public: X3 (); X3 (Constx3&) =Delete;//declare copy constructor as deleted functionx3&operator= (ConstX3 &) =Delete;//Declaration Copy assignment operator is deleted function};//the "=delete" function attribute can also be used to disable some conversion constructors of a class, thus avoiding undesirable type conversionsclassx4{ Public: X4 (Double) {} X4 (int) =Delete;};//the "=delete" function attribute can also be used to disable the new operator for some user-defined classes, thus avoiding the creation of objects in the free-store classclassx5{ Public: void*operator New(size_t) =Delete; void*operator New[] (size_t) =Delete;};voidmytest () {X4 obj1; X4 Obj2=obj1;//error, copy constructor disabledX4 obj3; Obj3=obj1;//error, copy assignment operator is disabledX5*PA =NewX5;//error, the new operator is disabledX5 *PB =Newx5[Ten];//error, new[] operator is disabled return;}intMain () {mytest (); System ("Pause"); return 0;}
C++11 class default function control: "=default" and "=delete" function void fun () = default; void Fun () =delete;