Highlights of the effective C + + (II.)

Source: Internet
Author: User

"Effective C + +" Chapter II: Construction/destructor/assignment operations
  1. A function written by C + + by default. The C + + compiler generates a default version of the class if it does not find the following function:
    1) Default constructor
    2) Default destructor
    3) Copy Constructor
    4) Copy assignment operator (= operator)
    The first two functions are not always generated, it only occurs when the compiler needs it. The latter two are only guaranteed to copy non-static members in bitwise semantics, so if a class with pointers, references, etc. non-static members either rejects the compiler-generated version or implements the two functions with care.
  2. If you do not want to use a function that the compiler automatically generates, you explicitly reject it. The key is to clarify whether the class being written allows the existence of these default functions, and if it is determined not to allow it, it can be declared as private access in c++98, and c++11 can also be declared as deleted. The best practice is to do a generic rejection of the compiler to generate a default function of the base class, so that a class that does not need a default function inherits from it.
  3. Declares a virtual destructor for the polymorphic base class. If the destructor of the base class is not virtual, then there is no destructor in the base class that actually needs to be called, so when you delete a base class pointer to the derived class pointer, the result is naturally not deterministic (usually a destructor is incomplete).
  4. Do not let exceptions escape destructors (such as returning resources). If an exception is thrown in the destructor and is not handled, then the destructor is likely to be undone, even if the exception it propagates is caught and handled by the upper function and cannot be done for the object that throws the exception. So if an exception occurs in a fictional function, either abort the program or handle the exception yourself (the destructor swallows the exception). Abort drop programs often make people "jump in the heart", so even though the "swallow the abnormal" is not perfect, it should be so. The practice is to write a function for an operation that might throw an exception and expose the function to the client, giving the client an opportunity to handle the exception itself. If the customer does not handle it themselves, do it in the destructor for the user.
  5. Never call the virtual function during construction and destruction. Constructors always call the constructor of the base class first, so calling a virtual function in a constructor is always unreasonable in semantics, because the virtual function at this point is not "virtual", the call is the parent version, the destructor is the same, although always after the call base class destructor, no, it should be said, precisely because of such a call order, Makes the virtual function "not virtual".
  6. Make operator= return a reference to *this. Doing so allows the object to be continuously assigned, allowing you to design a class that is more like a built-in type. It is possible to return a reference because *this is not a local variable, and *this will not be destroyed when it leaves the operator= function scope.
  7. Handle "self-assignment" in operator=. There are four basic requirements for writing operator=: Return reference to *this, with the const reference as the parameter passing way, to ensure that the depth copy semantics appropriate, processing self-assignment, that is, processing the customer to let an object = itself operation, This is a legitimate expression in the compiler's eyes, but the runtime is likely to make a mistake, and the solution is to determine the address and this equality of the objects referred to in the entry and exit parameters (which is a reference) between copy. There is also a higher requirement to ensure "exceptional security", which can usually be achieved through COPY-AND-SWAP.
  8. When copying objects, do not forget each part. This has what to say, if not done, it is not copied!! Note the copy semantics required to copy member variables (deep, shallow copies), and do not forget the members in all base classes. Do not let the copy constructor and the copy assignment function call each other, write a common copy function, and let the copy constructor and copy assignment function call it.

Highlights of the effective C + + (II.)

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.