#include <stdlib.h>#include<iostream>intMAX (Const intAConst intb);/** Const Qualifier: * 1. Define: Limit a variable not allowed to be changed, resulting in static effect * 2. Category: * (1) Top-level const: The limit variable itself immutable * Example: const int number;* int *const pointer;** (2) Bottom Const: The object to which the qualified variable points is immutable, and all references are the underlying const* example: const int *pointer;* const INT "ation;* 3. Usage: * (1) Define Constants * * (2) Protect modified things * * (3) Easy adjustment and modification of parameters * * (4) provides a reference for function overloading (* * *) (5) Save space and avoid unnecessary memory allocations * Const definition constants from the assembly point of view, just give the corresponding memory address, instead of the immediate number as shown in # define, so the const definition of the constant in the program run only one copy, and # Define defined constants have several copies in memory * * (6) Improved efficiency * Compilers typically do not allocate storage space for ordinary const constants, but instead store them in a symbol table, making it a constant during compilation, without the storage and read memory operations, making It's also highly efficient.*/intMain () {/** 1. Definition: Limit A variable is not allowed to be changed, resulting in static effect * 2. Category: * (1) Top-level const: The limit variable itself cannot be changed * (2) The underlying const: limited variable The object to which the amount is pointing cannot be changed, all references are the underlying const*/ Const intNum_1 =Ten; //num_1 = 20; //Error: Num_first is modified by const and cannot be changed Const int*num_pointer_1 = &num_1; Num_pointer_1+=8;//correct: The underlying const, the variable that the specified pointer refers to is immutableStd::cout << num_pointer_1 << *num_pointer_1 <<Std::endl; Const int&num_reference =num_1; //num_reference = 20; //Error: When you use the const modifier to reference a variable, it is the underlying constStd::cout<<Std::endl; Std::cout<<Std::endl; /** 3. Usage: * (2) easy to type check (compare with macro)*/ intNum_3_1 = A; intNum_3_2 = +; Std::cout<<"MAX (num_3_1, num_3_2) ="<< MAX (Num_3_1, Num_3_2) <<Std::endl; Std::cout<<"MAX (num_de_1, num_de_1) ="<< MAX (num_de_1, Num_de_1) <<Std::endl; }intMAX (Const intAConst intb) { returna > B?a:b;}
C + + Const qualifier