1. Const
The const is meant to be "invariant, constant".
C Language:
(1) Define the variable to be read-only and allocate memory "the constants here are just compiler attributes";
(2) combination of const and pointer: constant pointer, pointer constant, constant pointer constant;
In C + +:
(1) Define the variable as a read-only variable, no memory space "constant folding" before the address & and plus extern and the macro #define function;
(2) The constants in the class need to be initialized in the list, and the reference &;static const int A=10 can be directly assigned;
(3) Constant objects and constant functions. A constant object can only call a constant function and cannot modify a member variable; If you want to modify it, you need to add mutable keyword modification;
(4) The parameter is const, prevents the function to modify it, the return value is const cannot be the left value;
(5) Limit the scope in C + + to functions or files;
2, volatile
Volatile was meant to be "variable".
Volatile reminds the compiler that the variables defined later can change at any time, so the compiled program will read the data directly from the variable address each time it needs to store or read the variable. If there is no volatile keyword, the compiler may optimize read and save, may temporarily use the value in the register, if this variable is updated by another program, there will be inconsistencies. Mainly used to protect the security of variables.
Generally speaking, volatile is used in several places as follows:
(1), the variable which is modified in the Interruption Service program for other program detection needs to add volatile;
(2) A flag shared between tasks in a multitasking environment, such as data shared by several threads in multiple threads. should add volatile;
(3), memory mapped hardware registers are usually added volatile description, because each of its read and write may be different meanings.
3. Static
Static is meant to be "static".
C Language:
(1) The definition of a static variable, the local is to change its life cycle, the global is the transformation of its scope, to prevent the occurrence of the same name;
(2) Define a static function, the scope of the defined function is limited to the current file module;
(3) The static global variable is initialized to 0 and initialized only once;
C + + (uniqueness):
(1) The member variable belongs to the class, does not belong to any object, needs to initialize outside the class;
(2) member functions. Without this pointer, all objects share this variable;
(3) The static function cannot be declared as a virtual function;
4, mutable
Mutable's original intention is: "changeable, temperament uncertain".
Function: When you need to modify the data members of an object in the Const method, you can use the Mutable keyword before the data member to prevent compilation errors. Examples are as follows:
[CPP]View plain Copy class Cbook {public:mutable double m_price; If you do not add an error Cbook (double price): M_price (price) {} double getprice () const; Define Const Method}; Double Cbook::getprice () const {M_price = 9.8;//constant function to modify member variable; return m_price; } 5, explicit
Explict is meant to be "displayed."
Acts on the internal constructors of classes to prevent some implicit conversions;
For example:
[CPP]View plain Copy Class A {public:explicit a (int _size): size (_size=) {} private:int size ; Because the constructor of only one parameter is a type conversion function, the corresponding implicit type conversion may occur, plus explict will not.
A A1 (A);//ok
A A2=12;//error
A A3; Error, there is no default constructor;
A1=11;//error
A2=13;//error can not do the corresponding stealth conversion;
6. typeid-type identifier
A new feature of standard C + + is the RTTI (run-time type information run-time types information), which provides a standard way for the program to determine the object type at run time. Dynamic_cast and typeID.
Standard C + + provides a typeid () operation to get the type information, its arguments can be an expression, an object, a pointer, or a reference, by which you can get a pointer to a Type_info object that contains the necessary information about the type of the expression. The return result is a const type_info&. The Type_info classes implemented by different compilers are not the same. The features provided by the Type_info object are as follows:
(1), name (), you can get a string containing type information, return such as: "int", "MyClass", etc.;
(2), before (), used to traverse the list of types;
(3), = = operation, used to determine whether the type is the same;
T1 = = T2 Returns true if two objects T1 and T2 types are the same;
T1!= T2 Returns True if two objects have different T1 and T2 types, otherwise return false
T.name () returns the C-style string of the type, which is generated by a system-related method
T1.before (T2) returns the BOOL value indicating whether T1 appears before T2
Main function: Returns the pointer or refers to the actual type of the object.
If the typeid operand is not a class type or a class that does not have a virtual function, then typeid indicates the static type of the operand. If the operand is a class type that defines at least one virtual function, the type is evaluated at run time.
Examples are as follows:
[CPP]View Plain Copy
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.