Differences between macros and inline
Existing value, both of which are text replacement, reducing the number of program jumps and Improving Efficiency
1. Define is a pre-processing command and cannot be debugged. It can be replaced with the simplest text,
Inline is replaced during the compilation period. It can be debugged and exists.Parameter type check
2. When using inline, the function must be defined
Directly defined function, automatically converted to inline
How does define transform class variables ..,
//////////////////////////////////////// //////////////////////////////////////// ////////////////////
Memory alignment
Reason: Machine font length, addressing speed, computing speed, and platform transplantation reason
Principle: The starting address of each element is a certain multiple of sizeof (itself ).
Struct sets in struct. In principle, first alignment the inner struct and then alignment the external struct.
Union Max
Large-end short segment problem, TCP data packet conversion problem in Distributed Systems
//////////////////////////////////////// //////////////////////////////////////// ////////////////
Static_cast, dynamic_cast, const_cast, reinterpret_cast
Any problems can be solved by the compiler. Do not drag them to the runtime.
Upgrade 1
Char c = 123; int * P = (int *) & C; // Insecure code. The C compiler can run without making any judgment, but sometimes it may cause errors.
Strong conversion in C, replaced by static_cast <t> in C ++, performs security check during compilation. C: B, A; C is converted to a, or B, and the address is automatically calculated.
Upgrade 2
Sub-class parent class pointer conversion, static_cast <t> can do this, but it brings problems
If the subclass is converted to the parent class (large to small), the access may be out of bounds and an error is returned. This is a runtime error and cannot be processed by static_cast <t>.
As a result, dynamic_cast <t> appears, mainly used for downward conversion.
Dynamic_cast <source> (expression). The source and target must have an inheritance relationship, a virtual function, and a pointer and reference.
If it can be converted, it will be converted. If it cannot be converted, null will be returned (rtti, runtime type check)
Upgrade 3
Const_cast
In some cases, we cannot use the const variable. In this case, the const_cast attribute can be removed (only this time ).
Upgrade 4
Reinterpret_cast
Extremely dangerous, unaware of the value of existence
Inline Function, macro definition, memory alignment, type conversion