C + + Primer Chinese version of Learning notes (16) __c++

Source: Internet
Author: User
Chapter 17th Tools for large-scale programs 1 Large-scale applications often have the following special requirements:

More stringent uptime and more robust error detection and error handling.        Error handling often has to be done across multiple subsystems that are independently developed.                   ----exception handling, error detection and processing can be used to construct programs from a variety of libraries, which may contain independently developed libraries.                                                                                                                         ----namespaces can handle more complex application concepts. ----Multiple Inheritance

2 exceptions are thrown and captured in a manner similar to passing arguments to a function, you must be able to replicate objects of that type, so there is no exception for an array or function type. 3 Transfer control from throw to matching catch, there are two important meanings

Quit early on the function of the call chain. In general, the local store in the block that throws the exception does not exist when the exception is handled.

4 When an expression is thrown, the static compile-time type of the thrown object determines the type of the exception object.

Dereference the pointer in the throw, regardless of the object's actual type, and the type of the exception object matches the pointer's static type. The pointer to throw a local object is always wrong. When you throw a pointer, you must determine the existence of the object to which the pointer points when you enter the processing code.

5 Stack Expansion: Continue up the chain of nested function calls until a catch clause is found for the exception. 6 during stack expansion, the memory used by the local object is freed and the destructor of the class type local object is run, and the compiler does not normally undo the object of the built-in type.
7 The destructor should never throw an exception:

When a stack is expanded for an exception, if the destructor throws its own unhandled exception, it will result in the call to the standard library terminate function, terminate function to invoke the Abort function, and force an abnormal exit from the program. The static type of the exception descriptor in the 8 catch determines what action the catch clause can perform.
9 multiple catch clauses with types associated with inheritance that must be sorted from the lowest derived type to the highest derived type

try{
//use of the C + + standard library
}catch (overflow_erroreobj) {
//...
} catch (Construtime_error &re) {
//...
} catch (Exception) {/*...*/}

If an exception occurs between the new and the delete, the dynamically allocated memory is not freed. So we need an unusually secure programming method, namely RAII (Resource Allocate is initialization). Encapsulates the allocation and release of resources by defining a class. (Destructors run automatically).
One example of RAII is the Auto_ptr class:

To properly use the Auto_ptr class, you must adhere to the following restrictions imposed by the class:

Do not use a Auto_ptr object to hold a pointer to a statically assigned object, resulting in an undefined behavior when the revocation occurs. Never use two Auto_ptr objects to point to the same object. Do not use the Auto_ptr object to hold a pointer to a dynamically allocated array, releasing only one object delete instead of delete[] do not speak auto_ptr objects are stored in a container. Replication and assignment of Auto_ptr objects is destructive. After the Auto_ptr object is copied (or assigned), the original Auto_ptr object does not point to the object and the new Auto_ptr object (left operand) has the underlying object.

12 If the function is tangible as throw () description of the exception: do not throw any exceptions if there is no exception description: Can throw any type of exception
Using declaration using directive (try to avoid)
The 14 multiple inheritance constructor call order is neither affected by the base class that appears in the constructor initialization list nor by the order in which the base class appears in the constructor initialization list.
15 in order to solve the multiple inheritance of multiple base classes can lead to the occurrence of two semantic problems, leads to virtual inheritance.                                                
under Virtual inheritance, a shared base class child object is inherited for a given virtual base class, regardless of how many times the class appears as a virtual base class in a derived hierarchy.

            To avoid duplicate initialization problems: Specify that the constructor of the lowest-layer derived class initializes the virtual base class. 16  Constructs a virtual base class before constructing a non-virtual base class, regardless of whether the virtual base class appears anywhere in the inheritance hierarchy.

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.