Const in C/C ++, constin

Source: Internet
Author: User

Const in C/C ++, constin

1. the const object must be initialized because the value cannot be changed once created.

Eg:

Const int I = GetSize (); // correct: runtime Initialization

Const int j = 42; // correct: initialization at compilation

J = 33; // error: try to write a value to the const object

Const int k; // error: k is an uninitialized constant.

 

2. By default, the const object is only valid in the file.

To work in multiple files, define the const in only one file, and declare and use it in multiple other files.

Solution: For the const variable, no matter whether the declaration definition is added with extern, you only need to define it once.

Eg:

// File1.cc defines and initializes a constant, which can be accessed by other files

Extern const int bufSize = fcn ();

// File1.h

Extern const int bufSize; // The bufSize defined in file1.cc is the same

 

3. Bind the reference to the const object, which is called a reference to a constant.

Eg1:

Const int c = 1024;

Const int & r1 = c; // correct

R1 = 42; // error: r1 is a constant reference

Int & r2 = c; // error: try to point a constant reference to a constant reference

 

Eg2: int I = 42;

Const int & r1 = I; // const int & can be bound to a common int.

Const int & r2 = 42; // correct: r2 is a constant reference

Const int & r3 = r1 * 2; // correct: r3 is a constant reference

Int & r4 = r1 * 2; // error: r4 is a common uncommon reference.

 

A reference to const may reference a non-const object.

Eg: int I = 42;

Int & r1 = I;

Const int & r2 = I; // r2 binds object I, but cannot modify the I value through r2

R1 = 0; // r1 is not a constant. The value of I is 0.

R2 = 0; // error: r2 is a constant reference.

Reference a non-const object, that is, the object is a constant, so you can change its value through other ways.

 

4. const pointer

A pointer is an object that allows the pointer to be a constant. If the constant pointer must be initialized, the pointer value (the address in the pointer) cannot be changed.

Place * in front of const to indicate that the pointer is a constant, that is, the pointer itself is not the value pointed.

Eg: int errNumb = 0;

Int * const curErr = & errNumb; // curErr will always point to errNumb

Const double pi = 3.14159;

Const double * const pip = π // pip points to the constant pointer of the constant object

Read from right to left:

① Const is closest to curErr, which means curErr itself is a constant object.

② Next is *, and curErr is a constant pointer.

③ Int: the constant Pointer Points to an int object.

Similarly, pip is a constant pointer pointing to a double-precision floating-point constant.

 

A pointer itself is a constant, which does not mean that the value of the object it refers to cannot be modified through the pointer.

 

Eg: analyze the similarities and differences between int const * p, const int * p, int * const p, and const int * const p

Int const * p, const int * p are the same, p points to the address is variable, p points to the content is not variable,

Int * const p, p points to the address is not variable, p points to the content is variable

Const int * const p points to an address that is immutable, and p points to an immutable content.

 

5. Top-level const

The top-level const indicates that the pointer itself is a constant.

The underlying const indicates that the object referred to by the pointer is a constant.

Eg: int I = 0;

Int * const p1 = & I; // The p1 value cannot be changed, top layer

Const int ci = 42; // The ci value cannot be changed, top layer

Const int * p2 = & ci; // you can change the p2 value at the underlying layer.

Const int * const p3 = p2; // top-level const to the right, bottom to the left

Const int & r = ci; // The const used to declare the referenced is the underlying const.

 

Perform the object copy operation:

Top-level const is not affected: the Copied object value will not be changed, and no impact will be made.

Eg: I = ci; // correct: copy the ci value. The top-level const of the ci is not affected.

P2 = p3; // correct: p2 p3 points to the same object type. The top-level const of p3 does not affect

 

Bottom-layer const restrictions cannot be ignored: During object copy operations, the copied objects in and out of a volume must have the same bottom-layer const qualifications, or the data types of the two objects must be convertible. Generally, constants can be converted into constants, but not vice versa.

Eg: int * p = p3; // error: p3 contains the underlying const, p1 does not

P2 = p3; // correct: p2 p3 is the underlying const

P2 = & I; // correct: int * can be converted to const int *

Int & r = ci; // error: try to point a constant reference to a constant reference

Const int & r2 = I; // correct: r2 binds object I, but cannot modify the I value through r2

// (Const reference may reference a non-const object)

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.