Seeing the const keyword, the first thing a C + + programmer might think of is a const constant. This is not a good reflex. If you only know to define constants with const, then it is equivalent to using gunpowder only to make firecrackers. The
1, const understanding const is a keyword in C (c + +), and it is important to note that the const keyword changes a variable to a read-only variable. This variable is definitely not changed to a constant. That is, after the const-modified
C + + In the use of the Const keyword is very flexible, and using the const will greatly improve the robustness of the program, I based on all aspects of the information to summarize the following, I hope to help friends.
Const is a commonly used
Note: The const and typedef are very special when used together
Trap One:
Remember, a typedef is a new alias that defines a type, and unlike macro, it is not a simple string replacement. Like what:First define:typedef char* PSTR;And then:int
1. What is const?A common type is a type described by the Type modifier Const. The values of variables or objects of a common type cannot be updated. (Of course, we can steal the bar for updating :)
2. Why is const introduced?The initial purpose of
Talking about the const Qualifier and the const limitation
What is a const qualifier?
A Const qualifier is a constant qualifier. Objects modified by const have a constant nature and can only be read but cannot be written.
Why use the const
I. Use of const in C
From:Http://blog.chinaunix.net/u/26828/showart_667881.html
Const is a key word in C language. It specifies that a variable cannot be changed. Using const can improve the robustness of the program to a certain extent. In addition,
Tag: A modifier reference is passed how C + + fine memory allocation connection definitionThe use of the Const keyword in C + + is very flexible, and using the const will greatly improve the robustness of the program, I found in various aspects of
The use of the Const keyword in C + + is very flexible, and using the const will greatly improve the robustness of the program, I found in various aspects of the information summarized below, hoping to help friends.Const is a type modifier commonly
In C ++, the const qualifier is used to define constants. However, const can appear in the definition of constants or methods, and the positions of const are also different, the meanings are also different.
Use const whenever possible. Add the
The use of the Const keyword in C + + is very flexible, and using the const will greatly improve the robustness of the program, I found in various aspects of the information summarized below, hoping to help friends.Const is a type modifier commonly
Tag: The function parameter type is exactly the same as const ATI on () The This pointer defines andI. The role of const This is shown in the following table:
No.
Role
Description
Reference Code
1
You
The use of the Const keyword in C + + is very flexible, and using the const will greatly improve the robustness of the program, I found in various aspects of the information summarized below, hoping to help friends.Const is a type modifier commonly
One, possible combinations:(1) Const CHAR*P(2) Char Const*p(3) Char *const p(4) const char **P(5) Char Const**p(6) Char *const *p(7) Char **const pOf course, in (5), (6), (7) and then insert a const in a number of cases, but the analysis of the
I. About General constantsthe format of the declaration or definition is as follows:Const = ; [1] Const = ; [2]The definition of [1] and [2] is completely equivalent. For example:shaping Int (or other built-in type: Float,double,char)const int
Turn: A comprehensive summary of C + + constThe use of the Const keyword in C + + is very flexible, and using the const will greatly improve the robustness of the program, I found in various aspects of the information summarized below, hoping to
Const is used to protect program robustness and reduce program risks. The usage of const is complicated. It can be summarized as follows:
1: used when defining variables:
A: const int A = 100; the simplest usage means that variable A is a constant
There are 32 keywords in the ANSI C standard C language, namely:Auto break case Char const continue default does double else enum externfloat for goto if int long register return short SIG Ned sizeof static struct Switchtypedef Union unsigned void
1 start with const int IYou know, when declaring a variable, int I is like this; this I may be re-assigned to it. As follows:Int I = 0;//...I = 20; // The value is assigned again here.However, one day my program may need such a variable (temporarily
Staticstatic modifier local variablestatic modifier Local variables are used to modify where variables are stored, from automatic variables to static variables (space in the static area, not on the stack), but the link properties and scope of the
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.