C + + Primer (4th edition)-Study notes-part 5th: Advanced Topics

Source: Internet
Author: User

17th Chapter Tools for large programs
  1. Exception handling
    There is no exception for array or function type. Conversely, if an array is thrown, the thrown object is converted to a pointer to the first element of the array, similarly, if a function is thrown, the function is converted to a pointer to the function.

  2. Do not throw pointers

  3. Destructors do not throw exceptions

  4. In addition to the following possible differences, the type of the exception must match exactly with the type of the catch descriptor:
    • Conversions from non-const to const are allowed. That is, a throw of a non-const object can match a catch that specifies to accept a const reference.
    • Allows conversions from a derived type to a base class type.
    • Converts an array to a pointer to an array type, converting the function to an appropriate pointer to the type of the function.


  5. Name space
    Namespaces provide a more controllable mechanism for preventing name collisions, and namespaces can be partitioned into global namespaces, which makes it easier to use independently developed libraries.
    A namespace is a scope in which the author (and user) of the library avoids the limitations inherent in the global name by defining the name in the library within the namespace.

  6. definition of the namespace:
    namespace Cplusplus_primer
    {
             class Sales_ Item {/* ... */};
             sales_item operator+ (const sales_item&,  const Sales_ item&);
             class query  {/* */}; 
              class Query_base {/* ... */}; The name of the
    }
    namespace must be unique within the scope in which the namespace is defined. The
    namespace can be defined inside a global scope or other scope, but not within a function or class. After the
    namespace name is followed by a block of declarations and definitions enclosed in curly braces, you can place any declaration that can appear in the global scope in the namespace: classes, variables (and their initialization), functions (and their definitions), templates, and other namespaces.
    namespace scope cannot end with a semicolon.
    because different namespaces introduce different scopes, different namespaces can have members with the same name. The names defined in the
    namespace can be used directly by other members in the namespace, and code outside the namespace must indicate which namespace the name is defined in.

  7. Using namespace members from outside the namespace
    (1) Always use qualified name
    Namespace_name::member_name
    (2) write a using declaration to obtain direct access to the names we know will be used frequently:
    Using Cplusplus_primer::query;
    After this using declaration, the program can directly use the name Query without Cplusplus_primer qualifiers

  8. Namespaces can be discontinuous
    Namespaces can be defined in several sections. A namespace is made up of the sum of its separated definition parts, and the namespace is cumulative. A separate part of a namespace can be scattered across multiple files, and namespace definitions in different text files are cumulative.

  9. Separation of interfaces and implementations
    You can organize namespaces in the same way that you manage your own class and function definitions:
    (1) A namespace member that defines a class, and a function declaration and an object declaration that is part of the class interface, can be placed in a header file, and a file that uses a namespace member can contain the header files.
    (2) The definition of a namespace member can be placed in a separate source file.

  10. Global namespaces
    The name defined in the global scope (names declared outside any class, function, or namespace) is defined in the global namespace.
    Because the global namespace is implied, it does not have a name, so the notation:: Member_name refers to the members of the global namespace.

  11. Nested namespaces
    A nested namespace is a nested scope-its scope is nested inside the namespace that contains it.
    Names in nested namespaces follow general rules: the names declared in the perimeter namespace are masked by the declaration of the same name in the nested namespace. Nested namespaces are internally defined by names that are local to the namespace. Code outside the perimeter namespace can only refer to names in nested namespaces through qualified names.

  12. The names of the members in the nested namespace consist of the name of the perimeter namespace and the name of the nested namespace.
    For example, the name of the class declared in the nested namespace Querylib is Cplusplus_primer::querylib::query

  13. Unnamed namespaces
    Unnamed namespaces are not given a name when defined.
    Unnamed namespaces differ from other namespaces in that the definition of an unnamed namespace is local to a specific file and never spans multiple text files.

  14. Use of a namespace member--using a use declaration
    Using Std::map;
    A using declaration introduces only one namespace member at a time, which makes it very clear that whatever names are used in the program.
    The names introduced in the using declaration follow the regular scope rules. The name is visible from the using declaration point until the end of the scope that contains the using declaration. Entities with the same name defined in the outer scope are masked.

  15. namespace aliases
    A namespace alias can be used to associate a shorter synonym with a namespace name, for example:
    namespace primer = Cplusplus_primer;
    A namespace can have many aliases, and all aliases and original namespace names can be used interchangeably.

  16. Using indication
    using namespace Std;
    The using indication begins with the keyword using, followed by the keyword namespace, followed by the namespace name. If the name is not a namespace name that is already defined, an error occurs.
    The using indication makes all the names of a particular namespace visible, without restrictions. The short format name can be used starting from the using indicator point until the end of the scope with the using indication appears.

  17. argument-related lookups and class-type parameters
    std::string s;
    Getline (std::cin, s);
    std::string s;
    It finds and finds the Getline function defined by the string type in the namespace Std.

  18. Overloading with namespaces
    Functions that are members of two different namespaces cannot be overloaded with one another.
    The same namespace can contain a set of overloaded function members.

  19. Overloading and using declarations
    If a function inside a namespace is overloaded, then the using declaration of the function's name declares all functions that have that name. If there are functions for int and double in the namespace NS, then NS: the using declaration of the:p rint makes two functions visible in the current scope.

  20. Overloading and using indication
    The using indicates that a namespace member is promoted to a perimeter scope. If the namespace function has the same name as a function declared in the scope of the namespace, the namespace member is added to the overloaded collection:

  21. Multiple inheritance
    The constructors of a derived class can pass values to 0 or more base classes in constructor initialization:

  22. Order of Construction
    Constructor initializers can only control the values used to initialize the base class, and cannot control the construction order of the base class.
    The base class constructor is called in the order in which the base class appears in the class-derived list.

  23. Transformations with multiple base classes
    In the case of a single base class, a pointer or reference to a derived class can be automatically converted to a pointer or reference to a base class, as is the case with multiple inheritance, and a pointer or reference to a derived class can be converted to a pointer or reference to any of its base classes.

  24. Multiple base classes can cause ambiguity
    You can resolve ambiguity by specifying which class to use: Ying_yang. Endangered::p rint (cout);
    The best way to avoid potential ambiguity is to define a version of the function in a derived class that resolves two semantics.

  25. Virtual inheritance
    Virtual inheritance is a mechanism by which a class indicates that it wants to share the state of its virtual base class through virtual inheritance. Under virtual inheritance, for a given virtual base class, no matter how many times the class appears as a virtual base class in the derived hierarchy, only one shared base class child object is inherited. A shared base class sub-object is called a virtual base class.

  26. Special initialization
    Classes that inherit from classes that have virtual base classes have special handling of initialization. In a virtual derivation, the virtual base class is initialized by the constructor of the lowest-layer derived class, and the virtual base class is constructed first, and then the non-virtual base class is constructed.
    Any class that inherits a virtual base class directly or indirectly must also provide its own initialization for that base class.
  Chapter 18th tools for large programs
    1. C + + provides the following two ways to allocate and release raw memory that is not constructed
      (1) The allocator class, which provides a perceptual type of memory allocation. This class supports an abstract interface to allocate memory and then use that memory to save objects.
      (2) operator new and operator delete in the standard library, which allocates and frees raw, untyped memory that requires size.

    2. Allocator class

C + + Primer (4th edition)-Study notes-part 5th: Advanced Topics

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.