Transferred from http://www.cnblogs.com/yedaoq/archive/2011/04/29/2032597.html1#const_cast
Description from msdn :
A pointer to an object or an object member can be explicitly converted to the same type with different const, volatile, or
There are two ways to define constants in C #: compile-time constant and runtime constant ). The former is defined by "const", and the latter is defined by "readonly.
For a static constant (compile-time constant), the writing method is as
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.
Daily use of C-language constEvery day, I picked up a C-language shell, which grew up and formed thousands of miles.Today's shells: C-language const can improve program security and reliability to a certain extent.1. const modifies the read-only
Compiled correctlyCode:
# Include # include # include using namespace STD; Class t {public: T (string p) {ptext = P;} const char & operator [] (int pos) const {return ptext [POS];} string ptext ;}; int main () {string S = "ABCD"; t (s ); // T [
Const is a constant, readonly is read-only, and "blog Er" knows that :)
So what are their differences? My personal summary is as follows:
I. Assignment
1) The variable modified by const must be assigned an initial value during definition, and
1. General Constants
Int const x = 2; or const int x = 2;
Of course, X is also an array, such as X [5]; int can be replaced with the class name, and X is the object name, which is usually an object at this time.
If int is replaced with char *, It is
A const reference is a reference to a const object.
Copy codeThe Code is as follows: const int I = 10;
Const int & ref = I;
You can read the ref but cannot modify it. This makes sense, because I itself cannot be modified, and of course it cannot
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
1. The const modifier variable, which indicates that the value of the variable does not change:Const TYPE ValueName = value;2. The pointer uses the const(1) The pointer itself is constant, with the following two types of notation:(char*) const pName;
Record important c ++ learning records
1 const understanding:1. const modifies Common variables and pointersThe const modifier variable can be written in two ways:Const TYPE value;TYPE const value;
These two methods are essentially the same. It
[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]
Const is a keyword in C/C ++, but if it is used well, it can greatly improve the robustness of
Const defines a static value that is often assigned during object initialization. It cannot be changed later. It is a compilation constant.
Static defines a Static variable. You can change its value externally ..
Readonly is a read-only variable. It
Pre-knowledge1. Code Conversion Analysis Skillsin the early days some compilers translated C + + code into C code and then used the C compiler to build the executable file. One translation is to explicitly add the this pointer to the first parameter
"Reprint" Transfer from https://www.cnblogs.com/chogen/p/4574118.htmlConst is a C + + language qualifier that restricts a variable from being allowed to change. The use of const can improve the security and reliability of the program to some extent.
C + + references-temporary variables, reference parameters, and const referencesIf the actual participation reference parameter does not match, C + + generates a temporary variable. If the reference parameter is const, the compiler generates a
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
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
The above blog we have introduced the basic const, for this blog,
first, what is a reference:
A reference is a different name for the object, and a reference type refers to another type.
How to define:
To define a reference type by writing 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.