C + + Syntax-the only difficulty for const and compound types

Source: Internet
Author: User

Summary at the bottom.

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. The const meaning of RI is to treat CI as a variable.

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

Therefore, they (the standard-setting person) create a reference to the constant reference way.

under high energy!!!

Above says:

Treat CI as a 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: You cannot assign a value to an RI, but you can assign a value to I. Here I is the variable.

So: "Reference to a constant" is a reference | way |! For true constants, you must use this method for variables, which means that the code cannot be assigned by reference.

Think of it this way: you want to open an object that someone else can only read, but you can modify the value of this object.

int i; Your object

const int &RI = i; Pass this out.

You can bind to a constant reference: constant, variable ... Literal value, Expression!!!

int i = 5;

const int &RI1 = i; const int& bound to int variable

const int &RI2 = 9; Correct: The constant reference can be

const int &RI3 = r1 * 2; Correct: RI is a reference to a constant anyway

int &R4 = r1 * 2; Error: Normal citation is a fate.

R3 binding is the result of the expression at that time, it is a temporary amount.

Those things that are quoted and Const will be finished.

Nightmare, compound type There is another, pointer

3. Pointers and const

Good news: pointers and references are similar.

So:

Pointer to constant

const int i = 2;

const int *PCI = &i;

Pretty much, like, for constants, you must use pointers to constants.

Now point to the variable:

int II = 2;

const int *PCI = ⅈ

Similarly, you cannot assign a value after you dereference PCI, but you can assign a value directly to II.

Meal come!!!

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 that points to the object.

1. Must initialize 2. Can only point to one place.

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

int i = 0; Whether you want to refer to a variable or a constant

int *const CPI = &i; Will always point to I;

Big strokes:

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

const int *const CPCI = &i;

Parse: A constant pointer to a constant.

Property: 1. Must initialize (attributes from a constant pointer)

2. The point is no longer changed (attributes from a constant pointer)

3. You can refer to a constant or a variable (a property from a pointer to a constant)

5. Liberation

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

Defined:

Take a pointer to do an example

Top-Level const: Indicates that the pointer itself is a constant

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

Reasoning:

1. The reference does not have a top-level const, the reference is not an object, but a binding.

2. Constants of simple basic types are all top-level.

3. The pointer can be both a top-level const and an underlying const

Pit: Don't overlook the bottom cons T

int i;

const int *PC = &i;

int * p = pc; Error, the PC has the underlying const

Bottom-Level Const Usage Example: READ-only

int i;

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

About definition how to understand, personal view:

const INT | *const p;

Basic data type to Object | Declarator

Represents a pointer to a constant | Indicates that the pointer itself is a constant

Underlying const | Top-Level const

Note: The author second day learned, do not believe that someone can not understand

C + + Syntax-the only difficulty for const and compound types

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.