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

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <vector>
#include <map>

// Control of c++11 class default functions: "=default" and "=delete" functions

/*
C++ classes have four types of special member functions: default constructor, destructor, copy constructor, and copy assignment operator.
Special member functions of these classes are responsible for creating, initializing, destroying, or copying objects of the class.
If the programmer does not explicitly define a special member function for a class, but needs to use the special member function, the compiler will implicitly generate a default special member function for this class.
*/

// The C++11 standard introduces a new feature: the "=default" function. Programmers only need to add "=default;" after the function declaration to declare the function as a "=default" function, and the compiler will automatically generate the function body for the explicitly declared "=default" function. 
class X
{
public: 
    X() = default ; // This function achieves higher code efficiency than the user-defined default constructor 
    X( int i)
    {
        a = i; 
    }

private: 
    int a; 
};

X obj;

// The "=default" function feature is only applicable to the special member function of the class, and the special member function has no default parameters. 
class X1
{
public :
     int f() = default ;       // err , function f() is not a special member function of class X 
    X1( int , int ) = default ;   // err , constructor X1(int, int) is not a special member of X function 
    X1( int = 1 ) = default ;    // err , default constructor X1(int=1) with default parameters 
};

// The "=default" function can be defined either in the class body (inline) or out-of-line. 
class X2
{
public:
    X2() = default ; // Inline defaulted default constructor 
    X2( const X& );
    X2 & operator = ( const X& );
     ~X2() = default ;   // Inline defaulted destructor 
};

X2::X2( const X&) = default ;   // Out-of-line defaulted copy constructor 
X2& X2:: operator = ( const X2&) = default ;    // Out-of-line defaulted copy assignment operator


// In order to allow programmers to explicitly disable a function, the C++11 standard introduces a new feature: the "=delete" function. The programmer can disable the function by simply adding "=delete;" after the function declaration. 
class X3
{
public:
    X3();
    X3( const X3&) = delete ;   // declare the copy constructor as the deleted function 
    X3& operator = ( const X3 &) = delete ; // declare the copy assignment operator as the deleted function 
};

// The "=delete" function attribute can also be used to disable some conversion constructors of a class, thus avoiding undesired type conversions 
class X4
{
public:
    X4(double)
    {

    }

    X4(int) = delete;
};

// The "=delete" function feature can also be used to disable the new operator for some user-defined classes, thereby avoiding the creation of class objects 
class X5 in the free storage area
{
public:
    void *operator new(size_t) = delete;
    void *operator new[](size_t) = delete;
};


void mytest()
{
    X4 obj1;
    X4 obj2 =obj1;    // error, copy constructor is disabled

    X4 obj3;
    obj3 =obj1;      // Error, copy assignment operator disabled 

    X5 *pa = new X5;       // Error, new operator disabled 
    X5 *pb = new X5[ 10 ];   // Error, new[] operator disabled disabled

    return;
}


intmain ()
{
    mytest();

    system("pause");
    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324521397&siteId=291194637