[C ++ learning notes] const qualifier, learning notes const
The main reference for learning the const qualifier is the typical textbook C ++ primer. This document is purely a learning note, and the quotation marks in the sentence below are the
I. Static: static is a common modifier in C ++. It is used to control the storage and visibility of variables, next I will talk about the reason and role of the static modifier, and fully analyze the essence of the static modifier.
Static:
1.
Summary of const usage in C + +
author Jukevin
1. Const modifier Ordinary variables and pointers
const modifier variable, generally have two kinds of writing:
const TYPE value;
TYPE const value;
The two types of writing are essentially the same. It
1. pointer to the const objectWe can use a pointer to modify the value of the object it refers to. However, if the pointer points to a const object, it is not allowed to use a pointer to change the const value it refers. C ++ requires that the
(1) Example: 1. Int I = 1;Const int * cptr = & I;* Cptr = 2; // error. cptr points to constant 1.Cout 2. Int I = 1;Const int * cptr = & I;I = 2; // normalCout 3. Int I = 1, TT = 8;Const int * cptr = & I;Cptr = & tt; // normalCout To sum up, the only
View articles
C ++ static, const, and static const and their initialization
C ++ static, const, and static const and their initialization(17:27:47)Reprinted
Tags:Cstaticconst initialize it
the class's member function is followed by a const, which indicates that the function does not make any changes to the data members of the class object (accurately, non-static data members) .When designing a class, one principle is to add a const to
Today, a former colleague went to the interview. Come back and talk about it. What does const do after a function. I am confused. Detected knowledge vulnerabilities. Hurry up for tutoring. So find the following text from:
1. Const modifier common variables and pointersConst-Modified variables are generally written in two ways:const TYPE value;TYPE const value;The two formulations are essentially the same. The meaning of this is that the const-modified variant of type
A simple example
Int & B (int & n)
{
N ++;
Return n;
}
Int main ()
{
Int a = 10;
Int & B = B (a); // call B and pass the reference of.
// In B, n refers to,
// Return n; return;
// The reference of a is returned to B, and B is
// Operations on n
This is a tough thing to remember, because it has never been used. To sum up, I will not check it on C ++ primer later.
Const pointers are classified into three types:
1. pointer to the const object (1) definition form: const double * PT; // const
Defined
The Const keyword is scoped to the type of the variable. The value of a const-modified variable cannot be changed (it can no longer be assigned after initialization), for example:const int NUM1 = 1;NUM1 = 1; Errorconst indicates that
1. Top-level const and underlying const conceptThe pointer itself is an object, because the pointer actually corresponds to a piece of storage space of the memory unit, however, the pointer is also a data object, so the pointer is a constant and
Using the const method to use the c pointer (6) and the const method to use the const Method
(6) Use of const
Const in c expresses the meaning of "constant", which is more accurate to the meaning of "read-only. When a const encounters a pointer, it
Const is a commonly used type modifier in C + +, which refers to a type that is described using the type modifier const, and the value of a variable or object of a constant type cannot be updated.
1. Defining constants(1) The const modifier
Http://www.cnblogs.com/xudong-bupt/p/3509567.htmlC + + const allows a semantic constraint to be specified, and the compiler enforces this constraint, allowing the programmer to tell the compiler that a value is unchanged. If there is a value in
Const is a New Keyword introduced in C ++, which brings great convenience to C ++ programming. The pointer to the const object and the const pointer are two very similar concepts. They are very easy to confuse for beginners. 1. pointer to the const
Const usage summary in C/C ++
I.constBasic functions and usage
1. Declare the qualifier as read-only
The usage is as follows: add a keyword before/after the typeconst, The variable must be initialized; otherwise, the compilation is incorrect; the
Static is a common modifier in C ++. It is used to control the storage and visibility of variables. Next I will talk about the reason and role of static modifiers, the essence of the static modifier is comprehensively analyzed. Static: I. Control
I. Role of const:
1. Modify the value specified by const
Ex1.1:
Const int a = 2;
// A = 3; error: assignment of read-only variable
Just like a macro in C language, a will be put into the symbol list, but a is an internal link (internal link refers
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.