C + + const keyword

Source: Internet
Author: User

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; Error

const indicates that the variable is not writable, but this does not affect the readability of the variable:

const int NUM1 = 1;int num2 = NUM1; Correct, you can use a constant object for copy initialization

Const reference

A reference that uses a const adornment is called a reference to a constant, and is actually a reference bound to a constant object.

const int NUM1 = 1;const int &r1 = NUM1; correctly, the constant reference R1 is bound to the constant object Num1

It is important to note that a const reference cannot be bound to a constant object. This is because the objects whose bindings can be modified by means of a very good reference, so the very reference can only be bound to a very mass object.

const int NUM1 = 1;int &R2 = NUM1; Error, cannot bind a very R2 to a constant object num1

Although a const reference cannot be bound to a constant object, a constant reference can be bound to a very mass object.

int num2 = 1;const int &r1 = num2; correctly, the constant reference R1 is bound to the num2 on the very mass object

With respect to the binding of references (constants, very quantities) and objects (constants, very much), we can understand that references are just aliases, we use references to modify their bound objects by reference, and we do not modify constants to reference bound objects, so a constant reference can be bound to a constant object. You can also bind to a very mass object, whereas a very-mass reference can only be bound to a very-mass object.

Since a constant reference does not modify its bound object, it is also possible to bind a constant reference directly to a literal or to a general expression:

const int &R1 = 1; Correct, the value of the constant reference R1 binding is 0

Const pointer

A const-decorated pointer is called a constant pointer, and a constant pointer must be initialized, and once the initialization is complete, its value (the address stored in the pointer) can no longer be changed.

int num3 = 1;int *const P1 = &num3; P1 will always point to num3

It is important to note that the pointer pointer itself is a constant and does not mean that the value of the object it refers to cannot be modified by the pointer.

int num3 = 1;int *const P1 = &num3; P1 will always point to NUM3*P1 = 2; After this sentence ends, the value of num3 becomes 2.

So how do you make the value of the object that the pointer refers to cannot be modified? As long as the const adornment object is used, the pointer's definition statement is preceded by a const:

const int NUM4 = 1;const int *const P2 = &num4; P2 will always point to NUM4*P2 = 2; Error, cannot modify NUM4 value num4 = 2; Error, cannot modify the value of NUM4

There are two const in the definition statement of P2 in the above code, the meaning of the two const is different, the first const means that the value of the object pointed to by the pointer is not modifiable, the second const means the pointer itself (the pointer is also an object, and its value is the address value it stores) is non-modifiable. The different meanings of these two const are the top-level const and the underlying const, to be said below.

Top-level const and underlying const

The top-level const means that the pointer itself is a constant, that is, the pointer always points to an object.

The underlying const means that the object that the pointer refers to is a constant, that is, the object that the pointer refers to is not modifiable.

int num4 = 1;int *const P2 = &num4; Top layer Constconst int *p3 = &num4; Bottom Constconst int *const P4 = &num4; is both the top-level const and the underlying const

References can be thought of as the underlying const, and as with references, you cannot point a non-underlying const pointer to a constant object:

const int NUM5 = 1;int *P5 = &num5; Error, cannot point non-underlying const pointer P5 to constant object Num5int *const  P6 = &num5;//error, cannot point non-underlying const pointer P6 to constant object NUM5

Const member functions

In the definition of a C + + class, it is possible to add const,const to the parameter list of a member function to modify the type of the implicit this pointer.

What is an implicit this pointer? When we call a member function through an object, how does the member function know which object is calling it? In fact, the member function contains an additional implicit parameter named this, and when we call a member function, the member function initializes this with the object address that requested the function. Let's look at the following code:

Class person{   private:         int height =;         int weight =;   Public:         int getheight ()//ordinary member function         {              return height;//equivalent to return this.height;         }         int getweight () const//CONST member function         {              return weight;//= return this.weight;         }}; Person Person1;int h1 = Person1.getheight (); Correct, the Person1 address is implicitly assigned to the GetHeight function's implicit parameter thisint w1 = Person1.getweight (); Correct const person Person2;int h2 = Person2.getheight (); Error, cannot call a normal member function on a constant object int w2 = Person2.getweight (); That's right

Once the object is created, this will always point to the object, so this is actually a constant pointer (top-level const).

This is a top-level const (person *const this) by default, and the following code demonstrates the implicit initialization statement when Person1 and Person2 call the GetHeight function.

The following uses P1 to represent the Person1 pointer, p2 represents the Person2 pointer person *const this = person *p1; Person1 calls the GetHeight function, the correct person *const this = Const person *P2; Person2 Call getheight function, error

When a constant object Person2 calls a normal member function getheight, an implicit initialization statement attempts to assign a constant object to a very large object (the underlying), so an error is raised. In this case, if the this pointer is the underlying const, our initialization statement is no problem, and the const member function is here. The following code shows the implicit initialization statement when Person1 and Person2 call the Getweight function.

The following uses P1 to represent the Person1 pointer, p2 represents the Person2 pointer const person *CONST this = person *p1; Person1 calls the Getweight function, the correct const person *const this = Const person *P2; Person2 calls the Getweight function correctly

Summarize

    1. Const-Modified variable values are not writable, but readable;
    2. Whether it is a reference or a pointer, the very amount can be converted to a constant, but vice versa, that is, the constant reference/pointer can point to a non-const object, and the very reference/pointer can not point to a constant object;
    3. The top-level const means that the pointer itself is not mutable, and the underlying const is the object that the pointer points to is immutable;
    4. Const in the const member function can modify the type of the implicit this pointer.

Reference documents

"C + + Primer" 5th edition

C + + const keyword

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.