C + + notes: const__c++

Source: Internet
Author: User
Tags constant
Const Constants

Definition can not be modified, so the definition must be initialized : const std::string hi = "hello!"; Const Object Scope

Non-const objects defined in the global scope can be accessed throughout the program, and extern is the default. The [Code1] Const object defaults to the local variable of the file, and for the const variable to be accessible in other files, it must be specified as extern. [Code2]

constant Expression Const expression is an expression in which the value does not change and the result is computed at compile time an object (or expression) is not a constant expression determined by its data type and initial value [Code4] literal is a constant expression A const object initialized with a constant expression is also a constant expression.

Some of the const definitions in the header file

If the const variable is initialized with a constant expression , it should be defined in the header file (cause is similar to the C macro #define) If the const variable is not initialized with a constant expression, Then it should not be defined in the header file The const variable should be defined and initialized in a source file, and an extern declaration should be added to it in the header file so that it can be shared by multiple files

Reference a reference to a reference type cannot be defined , but any other type of reference can be defined. A reference must be initialized with an object of the same type as the reference. You must initialize when you define a reference, you can assign a value after initialization, but you cannot modify the reference's point.

Const Reference a reference generic reference to a const object cannot bind a const object const reference can be bound to a different but related type of object or bound to a right value. A [CODE3] non-const reference can only be bound to objects of the same type as that reference. CODE[3]

Const and Pointers pointer to const object (const int *ptr)

Allows the PTR to be assigned a value that points to another const object, but cannot modify the value of the object it refers to by PTR. You cannot use the void* pointer to save the address of a const object, and you must use the const void* to allow the address of a non-const object to be assigned to a pointer to a const object

The const pointer int *const curerr. Read the above definition statement from right to left as "Curerr is a const pointer to an int object" As with any const amount, the const pointer must also be initialized at the time of definition. and cannot point to another object after initialization

Const Pointer to const object const Double pi = 3.14159;const double *const pi_ptr = π You cannot modify the value of the object that the Pi_ptr points to, nor do you allow you to modify the pointer's point. Read the declaration statement from right to left: "Pi_ptr is first a const pointer, pointing to a const object of type Double."
typedef and Pointer typedef string *PSTRING;CONST pstring cstr;==string *const CStr; When declaring a const pstring, the const modifier is the type of pstring, which is a pointer. Therefore, the declaration statement should be defined as a const-pointer const qualifier that defines CSTR as a string object that can be placed either before or after a type. string const S1; = = Const string S2; [CODE5]
The const and member functions declare a member function as a constant double avg_price () const; A const member cannot change the data member const of the object it is manipulating must appear in both the Declaration and the definition, and a compile-time error occurs if it appears only in one place.

Returning *this from the const member function

In a normal, non-const member function, the type of this is a const pointer to a class type that can change the value pointed to by this, but cannot change the address saved by this.

In the const member function, the type of this is a const pointer to an object of the Const class type. You cannot change the object to which this is pointing, nor can you change the address saved by this.

you cannot return a generic reference to a class object from a const member function . A const member function can only return *this as a const reference.
Const and member function overloads screen& display (Std::ostream &os) {do_display (OS); return *this;} const screen& display (Std::ostream & OS) const{do_display (OS); return *this}
Const and iterator Const_iterator: can only be used to read elements within a container, but cannot change its value
===== Code1. Non-const objects defined in the global scope

file_1.cc
int counter;//Definition
//file_2.cc
extern int counter;//Uses counter from File_1
++cou nter;
Increments counter defined in File_1

Code2. A const object defined in a global scope

file_1.cc
//defines and initializes a const that's accessible to other files
extern const int bufsize = FCN () ;
file_2.cc
extern const int bufsize;//uses bufsize from File_1,extern marks BufSize as a declaration, so there is no initialization
//uses Bufsiz e defined in file_1 for
(int index = 0; index!= bufsize; ++index)
//...

Code3.const references can be bound to different but related types of objects or bound to right values

/*const references can be initialized to different types of objects or initialized to right values, such as literal constants: * *
int i =;
Legal for const references only
const int &r =;
const int &R2 = R + i;

/* The same initialization is illegal for non-const references and can result in a compile-time error. The reason is very subtle and worth explaining. It's easiest to see what
happens when you bind a reference to a different type. If we write * *
double dval = 3.14;
const int &ri = Dval;

/* The compiler converts the code into the following form: */
int temp = Dval;
const int &ri = temp;
Create temporary int from the "double
//bind RI to" that temporary/

* If RI is not a const, you can assign a new value to RI. This does not modify the Dval, but modifies the temp. Expect the assignment of RI to be modified Dval programmers will find that Dval is not
89 modified. Allowing only the const reference to be bound to a value that needs to be temporarily used completely avoids this problem because the const reference is read-only. */

Code4. An object (or expression) is not a constant expression determined by its data type and initial value

const int max_files = 20; Max_files is a constant expression 
const int limit = Max_files + 1;//limit is a constant expression 
int staff_size =//staff_size is not a constant expression 
C onst int sz = get_size (); SZ is not a constant expression/ 
* Although the initial value of staff_size is a literal constant, it does not belong to a constant expression because its data type is only a normal int instead of a const int. On the other hand, although the SZ itself is a constant, its concrete value is not obtained until run time, so it is not a constant expression. */

Code5.const qualifiers can be placed either before a type or after a type

string S;
typedef string *pstring;

The following three declarations are equivalent to the
const pstring CSTR1 = &s; 
pstring Const CSTR2 = &s; 

from:http://blog.csdn.net/liufei_learning/article/details/21219963

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.