(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
The const object cannot be changed.
Class A {public: int x, y ;}; class B {public: A * t; int * c, d ;}; void foo (const B & ob) {// ob. t ++; // invalid // ob. d ++; // invalid ob. t-> x ++; // valid * (ob. c) = 3; // valid}
In the above Code,
When a non-const variable is defined in the global scope, it can be accessed throughout the application. If it is a const variable, it is only a local variable in the current file.
We know that if you want to use a variable in other files, we
Create an object array in const object, NULL and nullptr, and C ++Zookeeper
1. defined as a class after const
# Include
Class area {public: int x; int y; mutable int z; // class member area (): x (10), y (10) not restricted by const ), z (2) {}
Mutable is an uncommon keyword in C ++. It can only be used for non-static and non-constant data members of a class.We know that the state of an object is determined by the non-static data member of the object. As the data member changes,The status
Attention
Const object implicitly feels the local variable of the file
When a non-const variable is defined in the global scope , it can be visited throughout the program. We can define a non-const variable in a file, and if the
Pay attention to this point
Const object defaults to a local variable of the file
When defining a non-const variable in the global scope , it can be accessed throughout the program, and we can define a non-const variable in a file,
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
[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
The use of const in C + + is very extensive, different location to use the meaning is not the same, so want to write an article to make a summary.
First of all, it is clear that the const is the basic meaning of "unchanging", but the same does not
VariableBuilt-in types
In C + +, the definition of a type is basically the same as the C language, except for some differences. In C + +, the return type of the main () function must be of type int, as described in C + + primer. This has not
The use of const in C + + is very wide, the meaning of the use of different locations is not the same, so want to write an article to do a summary. First, it is clear that const is the basic meaning of "unchanging", but it does not mean that
1. Define a const object
Const converts an object into a constant:
Const int bufsize = 512; Define the bufsize as a constant and initialize it to 512.
Since constants cannot be modified after definition, they must be initialized during definition.
The main reference to the Const qualifier is the classic textbook "C + + Primer", this article is purely study notes, the following quoted sentences are the words in the book.I C + + Foundation is not very strong, if there is any misunderstanding
There are two types of interaction between the pointer and the const qualifier: the pointer to the const object and the const pointer. It is not difficult to understand the meaning of the two types (which will be introduced soon below). However, in
(1) The non-const variable of the const variable defaults to extern. To make the const variable accessible in other source files, you must specify it as extern. Otherwise, an error occurs: error LNK2001: the external symbol "int const BUF_SIZE "(?
1. classes defined as const#include class area{public:int x;int y;mutable int z; class member without const constraint area (): X (Ten), Y (+), Z (2) {}void printxy () const //cannot access local variables in class {z = z + 1;std::cout go
Although, there is a certain C + + foundation (because the undergraduate has learned this kind, haha), but still decided to read the system of the book (before a few times before. Sweat).Leave it for your own reference later. (The content will be
ConstBy default, a variable is a local variable of the file that defines the variable. As we can see now, the reason for setting the default condition is to allow the constVariables are defined in the header file.
In C ++In some cases, constant
1.const is located on the left of the Modifier object; on the right side, the modifier pointer2.const only when the object is decorated, the const object's pointer itself can be changed, that is, the pointer address can be changed (pointing to
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.