1: The function is as follows:
(1) const is used to define constants: const-defined constants compilers can perform data static type security checks on them.
(2) Parameters in the form of the Const modifier: When the input parameter is a user-defined type and an abstract data type, the value pass should be changed to "const& Pass" to improve efficiency. Compare the following two sections of code:
void Fun (a a); void const &a);
The first function is inefficient. A temporary object of type A in the body of a function is used to copy parameter A, and the construction, copying, and destruction of temporary objects will consume time. The second function improves efficiency. "Pass-by-reference" does not require the creation of temporary objects, saving the temporal object's construction, replication, and time spent in the destruction process. But a reference to light can change a, so add a const.
(3) The return value of the const modifier function: If the return value of the function returned by the pointer is const, the return value cannot be directly modified, and the return value can only be assigned to a const-decorated pointer of the same type. For example:
Const Char *getchar (void) {}; Char *ch=getchar (); // Error Const Char *ch=getchar (); // correct
(4) Const modifier class member function (function definition body): Any function that does not modify the data member applies the const adornment, so that when the data member is accidentally modified or the non-const member function is called, the compiler will error. The const-decorated class has the form of a member function:
int GetCount (voidconst;
What is the role of const in preprocessing, const, static, and sizeof-c++ (say at least 3)