C + + Eighth day

Source: Internet
Author: User

Things to do today:

"C + + primer, 5E"

Pages 53rd to 58th, notes:

1, a very good reference must not bind a const object. Example const int CI = 1024; const int &R1 = CI;

2, constant references do not necessarily bind const objects. example int i = 42; const int &R1 = i;

A constant reference is similar to the holder of an object, restricting the manipulation of the object being held.

3, distinguish between pointer to const and const pointer declaration way. (read from right to left)

4, in fact, the above content with "top-level const" and "Bottom const" is easy to understand,

The underlying const for pointers to constants, the pointer itself can change values, but must point to constants.

You must have the same underlying const qualification to perform a copy operation.

The value of the top-level const cannot be changed, but it can be used as a literal.

problem encountered: do not understand "by default, const objects are only valid within a file"

After-school exercises:

Exercise 2.26

A is not legal, const object must initialize B legal

c Legal, the constant feature of CNT only works when performing operations that change CI.

D Not Legal


Exercise 2.27

Only A and F illegal

(a) int i =-1, &r = 0;

Illegal, the initial value of a non-amount reference must be an object

(b) int *const P2 = &i2;

Legal. Const pointer

(c) const int i =-1, &r = 0;

Legal, constant reference can bind literal value, general expression

(d) Legal

(e) Legal

(f) Unlawful

(g) Legal

Exercise 2.28

Right-to-left reading method

A. Illegal. CP is first a constant, not initialized

B. Not legal. P2 first is a constant and must be initialized

C. Illegal. IC not initialized

D. Illegal. P3 not initialized

E. Legal. P is a pointer to a constant, and the very amount

Exercise 2.29

A. Legal

B. Not legal. P3 is a constant pointer to a constant, P1 is a pointer to a variable, and if it is legal, it expands the operating range of the constant.

C. Illegal. Variable pointer cannot hold constant

D. Illegal. Cannot change the value of a constant

E. Illegal. Cannot change the value of a constant

F. Unlawful. Cannot change the value of a constant

Exercise 2.30

From left to right:

V2 top-level constant, V1-free variable, p1 no layer variable, r1 reference to V1

P2 underlying constant, p3 top-level constant, R2 is both the top-level const and the underlying const

Exercise 2.31

Mdzz This problem is not done, in short, with the same level of eligibility to assign value,

The top-level const cannot be assigned and must be initialized.

C + + Eighth day

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.