C + + Const qualifier

Source: Internet
Author: User

#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 &quotation;* 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

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.