c ++ 11 default class control function: "= default" and "= delete" function

Original https://www.cnblogs.com/lsgxeva/category/1107731.html

#define _CRT_SECURE_NO_WARNINGS 

#include <the iostream> 
#include < String > 
#include <Vector> 
#include <Map> // C ++ default function control class. 11: "= default" and "= delete" function / * 
C ++ classes there are four special member functions, which are: default constructor, destructor, copy constructor and copy assignment operator . 
Special member functions of these classes is responsible for creating, initialization, destruction, or copy of the object class. 
If the programmer does not explicitly define a particular member function to a class, but when the need to use special member function, the compiler generates a default for the class of special member function implicit. * / // C ++ 11 standard introduces a new feature: "default =" function. Programmers can add the statement after the function "= default;", the functions can be declared "= default" function, // compiler will explicitly declared "= default" Function automatically generating function thereof. class X- 
{ public : 
    X-()







= Default ; // This function gets the code efficient than the default constructor user-defined X-( int I) { A = I; } Private : int A; }; X-obj; // "default =" function characteristic It applies only to the special class member function, and that no special member function default parameters. class the X1 { public : int f () = default ; // ERR, function f () member function of a non-specific class X is the X1 ( int , int ) = default ; // ERR, constructor X1 (int, int) non-X special member function X1 (int = . 1 ) = default ; // ERR, default constructor X1 (int = 1) containing the default parameters }; // "default =" function may be (inline) class defined in the body, the class may be in vitro (out -of-line) is defined. class an X2 { public : an X2 () = default ; // Inline Defaulted default constructor an X2 ( const X-& ); an X2 & operator = ( const X-& ); ~ an X2 () = default ; // Inline Defaulted destructor }; an X2 X2 :: ( const the X-&) = default; // Out-of-Line Defaulted copy constructor an X2 :: an X2 & operator = ( const an X2 &) = default ; // Out-of-Line Defaulted copy assignment operator // In order to allow the programmer to explicitly disable a functions, C ++ 11 standard introduces a new feature: "= delete" function. Programmers can function in a statement after the "= delete;", the function can be disabled. class X3 { public : X3 (); X3 ( const X3 &) = Delete ; // declare a copy constructor function deleted X3 & operator = ( const X3 &) = Delete ; // declare a copy assignment operator function is deleted }; // "= Delete" function characteristic can also be used to disable certain class constructor conversion, so as to avoid undesired cast class X4 { public : X4 ( Double ) { } X4 ( int ) = Delete ; }; // " = delete "function characteristic class can also be used to disable the new operator some user-defined, so as to avoid creating a class of objects in the free store class X5 { public : void * operator new (size_t) = Delete ; void * operator new new [] (size_t) = Delete ; }; void mytest () { OBJ1 X4; Obj2 X4 = OBJ1; // error, the copy constructor is disabled X4 OBJ3; OBJ3 = OBJ1; // error, assignment operator copies are disabled X5 * PA = new new X5; // error, new new operator is disabled X5 * pb = new new X5 [ 10 ]; // error, new [] operator is disabled return ; } int main () { mytest (); System ( " PAUSE " ); return 0 ; }

 

Guess you like

Origin www.cnblogs.com/xiangtingshen/p/11025227.html