C + + const keyword

Source: Internet
Author: User

In general, the constkeyword is not used to differentiate the type of data, and in the declaration process, it acts as a cosmetic qualifier.

1 symbolic constants (usually, the values cannot be changed after the initial value is assigned, whereas constants must be assigned the initial number when defined)

int i; Const int 3 The ///identifier K represents the type of data that is stored in the memory unit, the keyword const-qualified, and cannot be modified by the identifier k to the int value in the represented memory unit

The memory units represented by I and K are all data of type int, but the const-qualified k is constant, and the symbolic constant must be assigned the initial value at the time of declaration.

The following attempt by declaring a pointer variable t refers to the memory unit represented by K, hoping to change the value of the constant k by the pointer T, but the result is very strange, the memory cell that T points to is the memory unit of the constant k, but the value is 2 different values

intMain () {//The amount K of a const-qualified declaration is constant, and the value represented by K cannot be changed    Const intK =3; int*T; T= (int*) &K; *t =7; cout<<&k<<endl;//0018ff14cout<<t<<endl;//0018ff14cout<<k<<endl;//3cout<<*t<<endl;//7    return 0;};

2. an array can be declared as a constant (meaning that each element in the array is treated as a constant)

Const Char " Hello "; An initial value must be assigned at the time of declaration, and the values of the array elements cannot be changed after initialization

3 regular member functions, constant data members

member functions using the const modifier--Constant member function

A const-qualified member function, which is a constant member function, cannot modify the data member value of the destination object (usually, cannot change the state of the object, mutable the data member of the keyword adornment, not the state of the object).

A non-const member function (ordinary member function) cannot be called in a constant member function because the normal member function exists to modify the state of the object

Data members with const adornments--Constant data members (constants in object instances-each object has its own copy, distinguished from static data members that are statically qualified, static data members--only one copy of the data in the entire class, used and maintained for all objects of the class)

The const keyword is used before the type of data member to characterize it, and the const restricts the modification of the data member, meaning that the data member cannot modify its value anywhere in the constructor initialization list, and then simply reads the value of the constant data member- --Here is the customary use of constants in classes

That is , a data member of a class can also be defined as a constant

#include"stdafx.h"#include<string>#include<iostream>using namespacestd;classconsttest{Private:    intA; Const intb//Regular data membersmutableintC; Public: Consttest (intAintBintc): b (b)//initialization of a regular data member, only in the constructor's initialization list    {         This->a =A; //this->b = b;//Error C2166:l-value Specifies const object, the value to the left of the equal sign is a constant object, and the constant object cannot be changed after the initial value is assigned         This->c =C; }    voidIncre () {a++; }    //The data member of the destination object cannot be modified in the constant member function    voidFun ()Const{C++; cout<<"a="<<a<<", b="<<b<<", c="<<c<<Endl; }};int_tmain (intARGC, _tchar*argv[]) {Consttest C_test (3,5,9);    C_test.fun (); //a=3, b=5, c=10C_test.incre ();    C_test.fun (); //a=4, b=5, c=11    return 0;}

4 frequently cited

When a declaration is referenced, a const adornment is used, and the declared reference is a constant reference, and the referenced object cannot be modified by a common reference to the declaration.

    intA =3; //the const-qualified declaration refers to K as a constant reference, and the value of the object it refers to cannot be changed by referencing K    Const int&k =A; //k = 9; //Error C2166:l-value Specifies const object    Const intb =5; Const int&t =b; //T = 9; //Error C2166:l-value Specifies const object

A constant reference, which can be bound to a normal object or bound to a constant object, but often refers to the object that is bound to be used as a constant object. This means that a constant reference to a reference base data type cannot be assigned or modified, nor can it modify the value of the data member of the referenced object (also including the normal member function that cannot be called) for a reference to a common reference to the class type

That is, through a constant reference, only the constant member function of the bound object can be called , it cannot call its ordinary member functions, of course, it is not possible to modify the data members of the bound object--a purpose is that the constant reference cannot change the state of its reference object, Because the oft-quoted object has been referred to as a constant object to see. A constant object is an object whose data member values cannot be changed throughout the lifetime of an object, and ordinary member functions cannot be called by regular objects, and interfaces that are common to an external interface are---Regular member functions .

Ordinary references, only reference (bound to) ordinary objects, to reference a common object to enforce type conversion (call the default assignment constructor completion, not determined here)

C + + const keyword

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.