Efficient C ++: constructor / destructor / Assignment

Learn C ++ default function calls and provide

  • The compiler will automatically create a constructor, a copy constructor, assignment operators, and destructors for each empty class
  • Do not use the function compiler automatically created to prevent this from happening, you have written these functions

If you do not want to use the function automatically generated compiler to explicitly rejected

  • The compiler provides a default function, not only can not bring convenience to the programmer, and will be difficult to locate the bug introduced in certain circumstances, and should therefore be explicitly rejected
  • Refusal way is to clearly state that the function is private and does not provide a function body
    • Control of private express authority to prevent external calls in unexpected circumstances
    • Does not provide a function body, it is to let the compiler error when linked, clearly suggest you pay attention to the function
class Uncopyable {
 Private : 
    Uncopyable ( const Uncopyable & ); 
    Uncopyable & operator = ( const Uncopyable & ); 
} 
// as the base class to inherit

Declaration of a polymorphic base class virtual destructor

  • If a class has a virtual function, it must be destructor Shen Mingcheng virtual function, mainly to prevent leakage of resources (such destructor sequentially performed from bottom to top)
  • If the class is not involved in a multi-state, then do not declare virtual function, virtual function call because the need to use virtual function list, call the high efficiency is not an ordinary function
  • If you do not want a class is instantiated, then class virtual function is declared pure virtual function, and provides its implementation
class the AbstractClass 
{ 
public :
     Virtual ~ the AbstractClass (); 
} 

the AbstractClass :: ~ the AbstractClass () 
{ 
    // function implemented
     // object is a resource release 
}

 

Abnormal elimination in the destructor

  • Destructor never spit abnormalities, which can lead to the release of part of the problem of resources, the destructor exception processing must try ... catch
  • If the customer needs to react to an action, and may throw, then you need to provide a common function to perform the operation, benefits of this design:
    • Provide common functions to customers, and the clear exception is thrown, allowing users the opportunity to make a reasonable deal with abnormal
    • Destructor resource release status check, final processing
class DBConn 
{ 
Private : 
    the DBConnect DBC; 
    BOOL Closed;
 public :
      void Close () 
    { 
        dbc.close (); 
        Closed = to true ; 
    }
    
     ~ DBConn () 
    { 
        IF (! Closed) 
        { 
            the try 
            { 
                dbc.close (); 
            } the catch (...) {
                 // stop or recording 
            } 
        } 
    }   
} 
// solve ran abnormalities may be close

Not call virtual functions in the constructor and destructor

  • See description, the constructor and destructor are not virtual function call; destructor itself can be designed to virtual functions, and in some cases, must be designed to virtual function
  • Before the constructor not completed execution, class vtable is not established
  • Before entering destructor, class vtable has been destroyed, can not be called

Let operator = return a reference to * this

Benefits: ease of continuous evaluation, implementation something like this:

= B = C = A D // continuous assignments
class Widget
{
public:
    Widget& operator+=(const Widget& rhs)
    {
        return *this;
    }
}

 

Guess you like

Origin www.cnblogs.com/chusiyong/p/11448264.html