Const and composite type _c languages in C + +

Source: Internet
Author: User
Tags constant

1.const and references

You can bind a reference to a constant, called a reference to a constant. You cannot assign a value to that reference.

Such as:

const int CI = 1024;
const int &ri = CI;

Interpretation: RI is a reference to CI. RI's const meaning is to see CI as a variable.

For constants, you can only use the reference to constant reference int &ri = CI; is wrong because CI cannot be assigned, but it may be assigned a value to RI to affect the const qualification.

So, they (standard-setting people) create references to constants.

Below high-energy!!!
The above says:

Depending on CI as variable

Why?

2. Referencing a variable to a constant

int i =2048;
Const &RI = i;

Interpretation: RI treats I as a variable and then references it

Effect: RI cannot be assigned, but I can be assigned a value. Here i is a variable.

So: "Reference to Constants" is a reference | way |! For real constants, you must use this approach, and for variables, this way the code cannot be assigned a value by reference.

So think: you want to open an object that others can only read, but you can modify the value of this object.

int i;  Your object is
const int &ri = i;  Pass this out.

You can bind to constant references: constants, variables ... literal value, Expression!!!

int i = 5;
const int &RI1 = i; const int& bound to int variable
const int &RI2 = 9;//correct: Constant reference can be
const int &RI3 = r1 * 2;//correct: Anyway ri is a reference to a constant 
   
    int &R4 = r1 * 2; Error: Common quote just accept it.


   

R3 is bound by the result of the request for this expression at the time, and is a temporary amount.

The references and the const things are over.

Nightmare, compound type also has one, pointer

3. Pointers and const

Good news: pointers and references are similar.

So:

A pointer to a constant

const int i = 2;
const int *PCI = &i;

It's like that. For constants, you must use a pointer to a constant.

Now point to the variable:

int II = 2;
const INT *PCI =ⅈ

Similarly, you cannot assign values after you dereference PCI, but you can assign values directly to the II.

Here Comes the!!!.

4.const pointer

Review: Pointers are objects, and references are not.

The const pointer means that the pointer object itself is a constant, allowing the pointer itself to be defined as an object.

Effect: The const pointer cannot change the address to the object.

Words: 1. You must initialize 2. Can only point to one place.

Putting * in front of the Const keyword indicates that the pointer itself is a constant.

int i = 0; You want to refer to the variable or constant
int *const CPI = &i;//will always point to I;

Big strokes:

The pointer to a constant and a constant pointer are separate.

const int *const CPCI = &i;

Analysis: A constant pointer to a constant.

Nature: 1. Must initialize (properties from the constant pointer)

2. Pointing is not going to change again (from a constant pointer to a property)

3. Can refer to both constants and variables (from the properties of pointers to constants)

5. Relief

The definition above is too much for the mouth. Good, that's why C + + is difficult.

Defined:

Take the pointer to do the sample

Top Const: Indicates that the pointer itself is a constant

Underlying const: Indicates that the object to which the pointer is pointing is a constant (or as a constant)

Reasoning:

1. References are not top-level const, references are not objects, just bindings.

2. Simple basic types of constants are top level.

3. Pointers can be both top and bottom const

Pit: Do not overlook the underlying const

int i;
const int *PC = &i;
int * p = pc; Error, PC has bottom const

Underlying const Usage Example: READ-only

int i;
const int *CI = &i; Get this out, just read it.

On the definition of how to understand, personal view:

const INT | *const p;

Point to the basic data type of the object | Declaration character

Indicates a pointing constant | Indicates that the pointer itself is a constant

Bottom Const | Top-Level const

Related Article

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.