[2014-11-24] High quality C++C Programming Guide-Read notes

Source: Internet
Author: User
Tags terminates

  1. The C + + language can use const to define constants, or you can use #define来定义常量. But the former has more advantages than the latter:
    • Const constants have data types, and macro constants do not have data types. The compiler can perform type safety checks on the former. Instead of only character substitution, there is no type safety check, and the substitution of characters can produce unexpected errors (marginal effects).
    • Some integrated debugging tools can debug const constants, but cannot debug macro constants.
  2. The constants that need to be exposed are placed in the header file, and no external constants are required to be placed on the head of the definition file. For ease of management, the constants of different modules can be centrally stored in a common header file.
  3. You cannot initialize a const data member in a class declaration. The following usage is incorrect, because the compiler does not know what the value of size is when the object of the class is not created.

    Class A

    {...

    const int SIZE = 100; Error, attempting to initialize const data member in class declaration

    int array[size]; Error, Unknown size

    };

  4. The initialization of a const data member can only be done in the initialization table of the class constructor
  5. Assert assert is a macro that works only in the debug version, and it is used to check what should not happen.
  6. Assert is not a function, but a macro. Programmers can think of assert as a harmless test that can be safely used in any system state. if the program assert is not meant to contain the Assert function has errors, but the caller has made a mistake, assert can help us find the cause of the error.
  7. Some of the rules cited are as follows:

    (1) The reference is created and must be initialized (the pointer can be initialized at any time).

    (2) cannot have a null reference, the reference must be associated with a valid storage unit (the pointer can be null).

    (3) Once a reference is initialized, the referenced relationship cannot be changed (the pointer can change the object at any time).

  8. There are three ways to allocate memory:

      (1) Allocation from a static storage area. Memory is allocated at the time of program compilation, and the entire running period of the program is present in this block. For example, global variables, static variables.

      (2) Create on the stack. When executing a function, the storage units of local variables within the function can be created on the stack, which are automatically freed when the function is executed at the end. The stack memory allocation operation is built into the processor's instruction set and is highly efficient, but allocates limited memory capacity.

      (3) Allocation from the heap, also known as dynamic memory allocation. When the program is running, it uses malloc or new to request any amount of memory, and the programmer is responsible for freeing the memory with free or delete. The lifetime of dynamic memory is determined by us and is very flexible to use, but the problem is the most.

  9. If the parameter of the function is a pointer, do not expect to use the pointer to apply dynamic memory.
  10. If you have to use pointer parameters to request memory, you should instead use the pointer to pointer
  11. Since the concept of pointers to pointers is not easy to understand, we can use function return values to pass dynamic memory.
  12. Do not return a pointer to "stack memory" with a return statement
  13. (1 The pointer dies, and does not indicate that the memory it refers to is automatically freed.

    (2) The memory is released, does not mean that the pointer will die or become null pointer.

  14. The "Wild pointer" is not a null pointer, it is a pointer to "junk" memory.
  15. There are two main causes of the "wild pointer":

    (1) The pointer variable is not initialized. Any pointer variable that has just been created does not automatically become a null pointer, and its default value is random, and it can be arbitrary. Therefore, the pointer variable should be initialized at the same time it is created, either by setting the pointer to null or by pointing it to legitimate memory.

    (2) The pointer p is not set to null after the free or delete, which makes the person mistakenly think P is a valid pointer.

  16. There are typically three ways to handle "memory exhaustion" issues:
    • Determines whether the pointer is null, and if so, terminates the function immediately with a return statement.
    • Determines whether the pointer is null, and if so, terminates the entire program immediately with exit (1).
    • Set exception handling functions for new and malloc.
  17. For more than 32-bit applications, the "Memory exhaustion" error handler is useless. Because the 32-bit operating system supports "virtual storage", memory ran out, automatically replaced with hard disk space.
  18. All unary operators

    Recommended overloads for member functions

    = () [],

    can only be overloaded as member functions

    + = =/= *= &= |= ~=%= >>= <<=

    Recommended overloads for member functions

    All other operators

    Recommended overloads for global functions

  19. The preprocessor replaces the function call by copying the macro code, eliminating the process of parameter compaction, generating assembly language call invocation, returning parameters, and executing return, thus improving the speed. The biggest disadvantage of using macro code is error-prone, and the preprocessor often produces unintended marginal effects when copying macro code.
  20. In a C + + program, you should replace all macro code with an inline function, and assert assert is probably the only exception.
  21. member functions defined in the class declaration will automatically become inline functions
  22. The C + + compiler will automatically generate four default functions for a, such as
  23. A (void); Default parameterless constructor

    A (const a &a); The default copy constructor

    ~a (void); Default destructor

    A & operate = (const a &a); The default assignment function

    (1) If the use of "default parameterless constructor" and "default destructor" is tantamount to abandoning the opportunity of autonomous "initialization" and "purge", the C + + inventor Stroustrup's kindness was wasted.

    (2) The "Default copy constructor" and "Default Assignment function" are implemented in the same way as "bit copy" rather than "value copy", and if the class contains pointer variables, these two functions are doomed to error.

[2014-11-24] High quality C++C Programming Guide-Read notes

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.