Relive the C + + Primer again
The first chapter, the beginning
1. The GNU compiler uses g++
$ g++-O prog1 prog1.cc
2.
while (Std::cin >> value)
- When using a IStream as a condition, if EOF is encountered, or invalid input (mismatch type) is judged false, jumps out
Chapter II: Variables and basic types
First, the basic built-in type
1. How to choose the type
- Clearly know that the impossibility is negative, defined as unsigned type
- Integer operation using int type
- Floating-point operations use double
- If you need a small integer, be clear signed char or unsigned char
2. An expression containing an unsigned type, which is converted to an unsigned operation by default when added to a signed int
- So it's important to be careful not to use this unsigned type expression as a judgment and possibly go into an infinite loop
- Do not mix symbolic and unsigned types
Second, the variable
1. Initialize! = Assignment
int 1; // Initialize int b; 2; // Assign Value
- It is recommended to initialize each variable of a built-in type
2. Declaration and definition of variables
int i; // declared and defined; default initialized to 0 extern int J; // declaration Not defined
- Inside the function body, a variable that initializes an extern keyword throws a duplicate definition of the error (even if the variable is not defined globally)
- The main function is also a function, so a variable of the extern keyword cannot be initialized in the main function.
3. Specification of variable names
- Variable names are usually in lowercase letters
- Custom class names usually start with uppercase letters
- If the identifier is made up of multiple words, use the Hump method
- Functions and methods begin with a verb, the first letter capitalized in Hungarian law
4, the definition of the variable, C + + in the use of the place before the definition is OK, C must be defined at the beginning of the function
5. Conforming type (int *, int &): Pointers and references
- A reference is a binding of another variable, which can be simply understood as another name of the variable, and the real may be a *const pointer
- The reference itself is not an object, so reference references cannot be defined (int &&i not)
- The referenced type must match exactly with the object being bound
- References are not objects, no actual addresses, pointers to references cannot be defined
6, const qualifier
- A const object cannot be changed once it is created, so a const object must be initialized
- The const variable adds extern, whether it is a declaration or a definition, so it needs to be defined only once.
7. Initialization and const references
- When a constant reference is initialized, an arbitrary expression is allowed as the initial value, as long as the expression can be converted to a referenced type: The reference uses the intermediate temporary variable
8, the top-level const and the underlying const
int 0 ; Const INT *const p = i; // The first one is the bottom and the second is the top level. Const int &r = i; // The const used to declare the reference is the underlying const
The left side of the = sign must be a change in the left value
The left and right sides of the = number can limit the assignment to a limited size (the more const the greater the limit)
9. Type alias: typedef = using
10, Auto: Let the compiler automatically recognize, but must have the initial value;
11, Decltype: The type of the variable is introduced by an expression
12. Prevent duplicate inclusion of header files (#pragma once can be used in VS)
#ifndef sales_data_h #define sales_data_hint main () {...} #endif
C + + Primer