Lesson 12 special member functions of generation mechanism

A Special member functions

(I. Overview

  1. Special member function refers to C ++ member function generated by itself, there are six species: default constructor, destructor, copy constructor, assignment copy function, and a mobile mobile constructor function assignment .

  2. Default particular member function generated public has access and is non-inline virtual function (destructor other exceptions). Typically, these functions are not used if the relevant code, the compiler does not generate its real function code .

  3. If the base class virtual function is a destructor, the compiler generates a derived class destructor is a virtual function.

  4. The default copy or move operations to refer to non-static members, they are "copied by member" or "members of the press move" . The "movement by member" is actually more like a moving member according to a request , because some type of operation does not have moving, to achieve these objects will "move" through which the copy operation.

(B) generating mechanisms C ++ 11

 

  1. The default constructor : constructor only when no user class declarations ( containing no parameters / parameterized constructor configured to move and copy constructors ) it generates.

  2. destructor : destructor only when the base class virtual, derived class destructor is virtual. Which is substantially the same as mechanism 98 in C ++. The only difference is that the destructor default noexcept.

  3. Copy constructor : generating condition ( class is not declared copy constructor; ② movement operation class is not declared ; ③ class is not declared a copy assignment or destructor ). Note that, when the current copying assignment statement or destructor, still generate a default copy constructor, but the behavior is gradually discarded.

  4. Copy mutator : generating condition (① class is not declared copy assignment; ② movement operation class is not declared ; ③ class is not declared copy constructor or destructor). Note that, when the current statement copy constructor or destructor, still generate a default copy assignment function, but the behavior is gradually discarded.

  5. Mobile Mobile construction and assignment functions : class only if the copy operation does not contain the user's statement, the moving operation and destructor other three conditions are generated when satisfied.

Second, understand the formation mechanism

The reason behind (a) generating mechanism

  1. Copy operations are independent of each other, i.e., in which a declaration, and does not prevent the compiler generates another.

   Early compiler that the default copy constructor and copy assignment are by members of the replication. Therefore, it is still willing to offer this most simple "shallow copy" mode for calls (in case of a member variable is referenced or constant, you can have the opportunity to prompt an error message). If the need for "deep copy" shall be defined by the user corresponding function.

  2. Move operation is not independent of one another, i.e. one declared, will prevent the compiler generates another.

   Assuming that if you declare a constructor move, actually represents a move operation with the compiler-generated by default "by members of the movement" are different. And if the "press members move" operation does not meet the requirements of the assignment carried out by members of the movement is also very and may not meet the requirements.

  3. Copy operations and move operations affect each other

    ① If you declare the copy operation (either the copy constructor or copy assignment) behavior, which indicates that the object replication by member does not meet the requirements. The compiler that since copied by member does not meet the requirements, "according to members of the movement" is very likely not meet the requirements.

    ② On the other hand, if you declare the move operation (moving or mobile construction assignment), it means "by members of the movement" does not meet the requirements, but also no reason to expect that compliance by members of the replication.

(B) the junior law and reasoning

  1. junior law (the Rule of Three) : If specified copy constructor, assignment, or any of a copy destructor, you have to declare all three simultaneously .

    ① In a copy operation, resource management, is also very likely also need to be replicated in another operation.

    ② class destructor will be participating in the resource management (or Release).

  2. Corollary

    ① If destructor declared , the ordinary copying by suitable class members. That copy operation will not be automatically generated , because their behavior is not correct. But 11 in C ++ copy operation remains generating function, merely for C ++ compatible with legacy code 98 and has , this behavior will gradually be discarded.

    ② If declared destructor , will not generate moving operation (since the destructor will inhibit the copy operation, the copy operation will prevent the generation of move operation).

(C) Note

  1. To eliminate the dependency generated by mutual inhibition, in C ++ by 11 " = default " to explicitly instruct the compiler generates a special version of the default member function.

  2. The template member function in any case will not inhibit the formation of a special member functions.

  3. have an explicit statement destructor, will prevent the generation of a move operation , the copy operation but still generated (gradually abandoned). This may cause side effects, that is, the original move operation could become the copy operation .

  The junior law principle, when declared destructor, should also generally provides replication functions and movement operations.

[Experimental] programming generated special member function mechanism

#include <the iostream> 
#include <Map> the using namespace STD; // 1. demonstrate the relationship between inhibition of particular member function
 // 1.1 custom default destructor prevents movement operation (including mobile and mobile structure assignment) class Foo 
{ Private :
     int I;
 public : 
    Foo () = default ; 
    Foo ( const Foo & F) = delete ;     // delete the copy constructor! Make the move operation will look for a function with a move operation instead of being copied to replace! 
    & Foo operator = ( const Foo &) = the Delete ; // ditto! 
    Foo ~ () {};   //

 




Here custom destructor! 
}; 

// 1.2 destructor custom make "moving operation" to "copying"! 
class StringTable 
{ 
    STD :: Map < int , STD :: String > values; 
    
    void makeLogEntry (STD :: String S) {}; // logging 
public : 
    StringTable () 
    { 
        makeLogEntry ( " Creating StringTable Object " );     
    }
    
     ~ StringTable ()    // note that this custom destructor, causes side effects! 
    { 
        MakeLogEntry ( " Destroying StringTable Object "  );
    }
}; 

// 2. Law junior 
class Bar 
{ 
public :
     Virtual ~ Bar () = default ;   // custom destructor, should also provide copy and move operations! 
    Bar (bar &&) = default ;      // providing mobile operation support 
    bar & operator = (bar &&) = default ; 

    bar ( const bar &) = default ; // providing mobile operation support 
    bar & operator = ( const bar &) = default ; 
}; 

// 3. special member function template 
class  Widget
{ 
public:
    template<typename T>
    Widget(const T& rhs) { cout <<"Widget(const T& rhs)" << endl; }

    template<typename T>
    Widget& operator=(const T& rhs) 
    { 
        cout << "Widget& operator=(const T& rhs)" << endl;
        return *this; 
    }
    
public:
    Widget() = default;    
};

intmain () 
{
    // 1. custom destructor, prevents movement operation 
    Foo F;
     // Foo :: Move STD = F1 (F);    // fail to compile! Since destructor custom default move operation is deleted.
    // f1 = std :: the Move (w);        // fail to compile! For the same reason. 
    
    ST1 StringTable; 
    StringTable ST2 = STD :: Move (ST1); // this is intended to move the constructor call, but the class destructor prevent generation
                                       // default move operation, the copy operation becomes instead. As for the values of complex objects
                                       // system, and td :: map <int, std :: string> copied more often than not moving much slower! 

    // 3. Special member function template does not inhibit the compiler-generated special member function of a default 
    Widget w; 
    Widget W1 = w; // call the default copy constructor generated by the compiler 
    Widget w2 =2; // call the function template: the Widget (const T & RHS) 
    
    W1 = w2 of; // call the default copy assignment functions generated by the compiler 
    W1 = 2 ;   // call the function template: the Widget & operator = (const T & RHS) 

    return  0 ; 
} 
/ * output 
the Widget (const T & RHS) 
the Widget & operator = (const T & RHS) 
* /

Guess you like

Origin www.cnblogs.com/5iedu/p/11294895.html