Valid C ++ function compiler, valid Compiler

Source: Internet
Author: User

Valid C ++ function compiler, valid Compiler
P5: understand which functions are compiled and called in C ++

1: The compiler can secretly create default constructor, copy constructor, copy assignment operator, and destructor for the class. Only these functions need to be called can they be created by the compiler.

2: If you want to support the value assignment operation in a class that contains "referenced members" or "const members", you must define the copy assignment operator by yourself.

3: If base classes declares the copy assignment operator as private, the compiler rejects generating the copy assignment operator for the subclass.

4: The Destructor generated by the compiler is a non-virtual function.

P6: if you do not want to use a function automatically generated by the compiler, you should explicitly reject it.

1: to reject the functions automatically provided by the compiler, the corresponding member functions can be declared as private and not implemented. Or use the base class method such as Uncopyable.

Class Uncopyable {protected: // allows derived Object Construction and destructor Uncopyable (){}~ Uncopyable () {} private: // However, copying Uncopyable (const Uncopyable &); Uncopyable & operator = (const Uncopyable &);};
P7: Declares virtual destructor for the polymorphism base class

1: If the Destructor is not virtual, when the subclass object is parsed by a base class pointer, its derived part will not be destroyed.

2: a base class with polymorphism should declare a virtual destructor. If the class has a virtual function, it should have a virtual destructor.

3: if the purpose of the class design is not to be used as a base class, or not to have polymorphism, virtual destructor should not be declared. STL containers, such as vector, list, set, and string, are non-virtual destructor.

4: The base class with polymorphism is designed to "process the derive object through the base class interface ".

5: the operating method of the Destructor is that the destructor of the class derived from the deepest layer is called first, and then the destructor of each base class is called.

6: The virtual destructor of an abstract class should have a definition, because Article 1 calls the abstract class destructor in the subclass's destructor.

Virtual ~ AWOW () = 0; // declare the abstract destructor AWOW ::~ AWOW () {}// definition of abstract destructor
P8: do not let exceptions escape the destructor

1: If the Destructor may throw an exception, capture the exception. A common function is provided to give users the opportunity to handle the operation by themselves.

Class DBConn {public: void close () // give the client a chance to handle abnormal behavior {db. close (); closed = true ;}~ DBConn () {if (! Closed) {try {db. close ();} catch (...) {// record and end the program or swallow the exception without processing. }}} Private: DBConnection db; bool closed ;};
P9: virtual functions are never called during construction and analysis.

1: During the base class construction of the deviced class object, the object type is only a base clas object rather than a deviced class object! The virtual function will only be parsed to the base class, and the object will not become a deviced class object before the start of the deviced class constructor. The same principle is true for destructor, except that the Destructor sequence is from deviced class to base class.

2: The deviced class can pass necessary information to the base class constructor to call the non-virtual function.

P10: Make operator = return a reference to * this

1: The value assignment operator returns a reference to * this, which can realize chained value assignment. For example, x = y = z = "str ";

P11: handle self-assignment in operator =

1: The operator ='s self-assignment is correct by means of "test with verification", statement sequence adjustment, copy-and-swap, etc.

2: Make sure that if the function operates on multiple objects and multiple objects are the same object, the behavior is still correct.

Class Widget {void swap (Widget & rhs); // exchange * this and rhs data. For details, see P29. BitMap * pb;}; Widget & Widget: operator = (const Widget & rhs) {if (this = & rhs) return * this; // verify the test Widget temp (rhs); // create a copy of The rhs data swap (temp); // exchange * this with the copy data; copy-and-swap Technology return * this ;}
Widget & Widget: operator = (const Widget & rhs) {BitMap * pOrig = pb; pb = new BitMap (* rhs. pb); delete pOrig; // This write aims to ensure that pb cannot be deleted before replication. Return * this ;}
P12: Do not forget every component of an object when copying it.

1: When writing the copying function, make sure to copy "all member variables in the object" and call the appropriate copying function in all base classes to copy "all base class components ".

2: Do not use a copying function to implement another copying function. Put the function in the third init function and call it by the copying function.

PriorityCustomer: PriorityCustomer (const PriorityCustomer & rhs): Customer (rhs) // call the copy constructor of base class {logCall ("copy constructor");} PriorityCustomer & PriorityCustomer :: operator = (const PriorityCustomer & rhs) {logCall ("Copy operator"); Customer: operator = (rhs); // assign values to the base class component priority = rhs. priority; retern * this ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.