Chapter 2 variables and Basic Types
Basic built-in types of zookeeper
Arithmetic types: the type of a numeric value (that is, an integer or a floating point number ).
⒉ ⒈ Integer
INTEGER (integer types): The arithmetic type of integers, characters, and boolean values. Including:
(1)Char: Bytes type, usually a single machine byte, minimum 8 bits
Wchar_t:
Wide fill type, minimum 16 bits
Extended character sets, such as Chinese and Japanese
(2)Short:
Short integer, usually half a letter long, with a minimum of 16 characters
Because the range is small and easy to cross-border, integer arithmetic operations are rarely used.
Int:
Integer. It is usually a single character with a minimum length of 16 bits. Most machines use 32 bits for representation.
Error-prone during Integer Operation
Long:
Long Integer, usually 1 or 2 machine characters long, minimum 32 characters
32-bit representation of a pairInt64-bit representation
LongThe cost of the latter is much higher than that of the former.
(3)Bool:
Boolean. When values are assigned, the value 0 indicates that the arithmetic type is false, and the remaining values are
True
Symbol (Signed) And unsigned (Unsigned) Type
(1) The signed type can be positive, 0, or negative. The unsigned type can only represent non-negative numbers.
(2)Int,Short,LongBoth are default
Signed
(3)CharThere are three different types: normal,
Unsigned,
SignedBut there are only two representation methods.
CharWhich of the following expressions is determined by the compiler?
The representation of the integer value of struct.
(1)UnsignedAll bits are used to represent numerical values.
(2)SignedThe compiler determines how to use a bit. A common strategy is to use one of the digits as the sign bit. If this digit is 1, the value is negative, and if it is 0, the value is 0 or positive.
When the handler assigns an out-of-bounds value to a specified type of variable:
(1) If the type isUnsignedThe compiler satisfies the requirements by modulo the out-of-range value.
(2) If the type isSignedThe behavior is undefined.
Floating Point
(1)Float:
Single-precision floating-point number. Generally, a single character is long and has a minimum of six valid digits.
Possible loss of precision cannot be ignored
(2)Double:
Double-precision floating point number, generally two characters long, with a minimum of 10 valid digits
It calculates the minimum price of 8 bits andFloatIt is basically not wrong to use it.
(3)Long
Double:
Extended Floating Point Number, generally three or four characters long, with a minimum of 10 valid digits
The extra precision provided by OSS is often unnecessary and requires additional operation costs.
Literal literal value constant
Returns the integral numeric nominal value.
(1) A literal value integer constant can be expressed in decimal, octal (starting with 0), or hexadecimal (starting with 0x or 0x ).
(2) The default value isIntOr
LongType (value-appropriateIntFor
IntType, Ratio
IntThe maximum and minimum 8 bits are
LongType)
(3) The value is suffixed with U, L, and UL, respectively.Unsigned,
Long,
Unsigned Long
⒉ Floating point numeric nominal value
(1) It can be represented by common decimal notation or scientific notation. When using the latter, use E or e to indicate the index
(2) The default value isDoubleType
(3) The value is suffixed with F, and l represents respectively.Float,
Long Double
Boolean nominal value: True, false
Character nominal value
(1) defined by characters or escape characters caused by single quotes
(2) Add L to the front to indicate the wchar_t type.
(3) escape characters defined in C ++ include:
/'Single quotes
/"Double quotation marks
// Backslash
/0 null characters
/A bell
/B back
/F paper
/N line feed
/R press ENTER
/T horizontal Tab
/V vertical Tab
/The OCT number is an OCT character (Oct indicates an octal number and does not have to start with 0)
/The xhex number is a hex character (hex represents a hexadecimal number, not starting with 0x or 0x)
Character string nominal value
(1) defined by several characters or escape characters caused by double quotation marks
(2) whose type ends with nullConst
CharArray (null characters at the end are automatically added by the compiler)
(3) add L to the front to indicate the wide string nominal value.
(4) two adjacent (WIDE) string nominal values separated by spaces, tabs, or line breaks can be connected to a new (WIDE) String Literal Value
This feature can be used to process long string denominations
Attempts to connect a string literal value and a wide string literal value will result in undefined behavior
In the ⑸ C ++ code, add the backslash symbol at the end of a line (no spaces or comments may appear later) this line can be processed as the same line as the next line (so normal indentation may not be allowed in the second line)
This feature can also be used to process long string literal faces, but is not commonly used.
Invalid parameters variable
Lvalue and rvalue)
(1) left value: the value that can appear on the left or right of the value assignment operation. NonConstThe left value is readable and writable.
(2) Right value: the value that can appear on the right but not on the left of the value assignment operation. Readable and writable.
Variable is left Value
Variable name, which can begin with a letter or underline and can contain letters, numbers, and underscores. It is case sensitive.
Variable definition
(1) The definition of a variable is to allocate storage space for the variable and specify its initial value. In a program, a variable has only one definition.
(2) variable Initialization
① Two initialization modes are supported when the initialization formula is provided:
A. Copy initialization: Use the equal sign (=) (Note: it is not a value assignment)
B. Direct initialization: Put the initialization formula in brackets; more flexible and more efficient
For built-in types, there is almost no difference between the two; for class types, sometimes the latter must be used
② Initialization is not provided:
A. For built-in type variables: the automatic initialization defined in the function body is 0, and those defined in the function body are not automatically initialized.
B. For Class type variables: if the class provides a default constructor, call it. Otherwise, An Explicit initialization type must be provided.
③ When multiple variables are defined at the same time, the preceding initialization methods (including those without initialization) can be mixed. You can use the values of the previously defined variables in the same definition to initialize the variables.
(3) defining variables at the first use of variables often increases code readability and reduces overhead.
Declare variable
(1) The declaration of variables indicates the type and name of the variables to the program. The statement can appear multiple times in the program. Definition is also Declaration
(2) UseExternA keyword can declare a variable without defining it. It is often used to make data span the scope of multiple files.
(3) If the Declaration has an initialization type, it is treated as a definition, even ifExternThe same is true for keywords.
(4) non-ConstThe object is visible in all files of the entire program and can be accessed as long as it is declared in other files;
But in the global scopeConstBy default, an object is the local object of the file in which it is defined. It must be specified before Definition
ExternIn order to access other files
The scope of the zookeeper name
Scope: A part of the program. The name makes sense in it.
C ++ has the following scopes:
(1) Global: The name is defined outside any other scope.
(2) Class: The name is defined by class
(3) namespace: The name is defined in the namespace.
(4) Local: The name is defined in the function.
Statement block: The name is defined in the statement block (that is, it is defined in a pair of curly braces)
Explain statement: the name is in the statement (for exampleIf,
While,ForStatement ).
The role scope can be nested, and the name of the outer scope can be seen in the internal layer. The name in the inner scope can block the same (hide) Outer
Zookeeper reference
A reference is a composite type (that is, a type defined by other types) that represents the alias of another (non-referenced) object, all its operations are directly applied to this object.
References are mainly used as function parameters.
Refer reference definition format is
Type name & identifier
(Definition Format of array reference: type name (& identifier) [dimension]
Pointer Reference Definition Format: type name * & identifier)
When multiple references are defined at the same time, each referenced identifier must be prefixed with & (and [dimension] And *)
The definition reference must be initialized with an object of the corresponding type, indicating that it is bound to this object and cannot be changed once bound.
BytesConstReferences are read-only and can be bound to objects of different but related types, or to the right value;
NonConstObjects can only be bound to objects of the same type as the reference.
⒉ Category definition type name
BytesTypedefDefines a synonym for a type. Definition Format:
TypedefType identifier;
Usually used for the following three purposesTypedef:
(1) Hide specific type implementations and emphasize the purpose of using types
(2) simplified type definition for understanding
(3) Use the same type multiple times to ensure that the purpose of each use is clear
Repeated bytes Enumeration
The enumeration type is composed of a group of enumerator members.
Each enumeration member has its own name and is initialized as an integer.ConstValue (the value can be repeated, but cannot be modified after initialization)
Definition Format:EnumEnumeration type name {list of enumeration members separated by commas };
You can use a constant expression to provide an initial value for some enumerated members in the list through the value assignment operator. If no initial value is provided, each value is 1 larger than the previous one (the first value is 0 by default)
EveryEnumOnly one type is defined. Only enumeration members of this type or another object can be used to initialize or assign values to enumeration objects.
Zookeeper header file
Because the header file is included in multiple source files, it should only contain object and function declarations but not definitions, except for the following:
(1) Class
(2) initialized using a constant expressionConstObject
(If it is not initialized using a constant expression, it should be defined and initialized in a file like other objects, and added to the header file.ExternDeclaration for multi-file sharing)
(3)InlineFunction
The above can be defined in multiple files (as long as the definitions are consistent ). The header file must be defined because the compiler needs their definition to generate code.
✓ Pre-processor
(1) # include only one header file name is accepted as a parameter.
Header file names are considered as standard header files in angle brackets, and the compiler will search
The header file name is considered to be a non-system header file in quotation marks. It is usually located in the location of the source file.
(2) You can use a Preprocessor to define header guard to avoid multiple inclusion, for example:
# Ifndef xxx_header_h
# Define xxx_header_h
// Header file content, including class definition
# Endif