Const usage Summary

Source: Internet
Author: User

A common type is a type described by the Type modifier Const. The values of variables or objects of a common type cannot be updated. Therefore, Initialization is required to define or describe the common type.
Const usage:
1 normal constant
2 object Constants
3. Common pointers and references. Use them as function parameters.
4. Regular data members

5 common member functions

1. General Constants

A constant is a constant of a simple type. When defining such constants, the modifier const can be used before or after the type specifier. For example:

Int const x = 2;

Or

Const int x = 2;

Define or describe a regular array in the following format:

<Type description> const <array name> [<size>]…

Or

Const <type specifier> <array name> [<size>]…

For example:

Int const A [5] = {1, 2, 3, 4, 5 };

Or

Const int A [5] = {1, 2, 3, 4, 5 };

 2. Regular objects

A constant object is an object constant. The definition format is as follows:

<Class Name> const <Object Name>

Or

Const <Class Name> <Object Name>

When defining a common object, Initialization is also required, and the object cannot be updated. The modifier const can be placed after the class name or before the class name.

3. Regular pointer

When the const is used to modify the pointer, the position of the const is different, but the meaning is different.

(1) constant pointer to the string: fixed to the address, and the content can be changed.

Char * const prt1 = stringprt1;

The value assigned below is invalid.

Ptr1 = stringprt2;

The following values are valid:

* Ptr1 = "M ";

(2) pointer to a String constant: The address can be changed and the content is fixed.

Const char * ptr2 = stringprt1;

The value assigned below is invalid.

* Ptr2 = "X ";

And:

Ptr2 = stringptr2;

It is legal.

Therefore, when using const to modify the pointer, pay attention to the position of Const.

4. Frequent references

The const modifier can also be used to describe the reference. The referenced object cannot be updated. The definition format is as follows:

Const <type specifier> & <reference Name>

For example:

Const double & V;

In practical applications, common pointers and common references are often used as function parameters. Such parameters are called common parameters.

In C ++ object-oriented programming, pointers and references are used a lot, and the constant pointers and references modified by const are used more. If a common parameter is used, it indicates that the function does not update the object pointed to or referenced by a parameter,In this way, you do not need to execute the copy initialization constructor during parameter passing.This will improve the program running efficiency.

5 common member functions

A member function that uses the const keyword for description. A common member function does not change the attributes of an object. Therefore, only common member functions are eligible to operate constants or common objects,Member functions without the const keyword cannot be used to operate on common objects.. Common member functions are described in the following format:

<Type description> <Function Name> (<parameter table>) const;

Here, const is the type modifier after the function description. It is an integral part of the function type. Therefore, the const keyword must also be included in the function implementation part.

# Include <iostream> <br/> using namespace STD; <br/> class r <br/>{< br/> PRIVATE: <br/> int R1, R2; <br/> Public: <br/> r (INT R1, int R2): r1 (R1), R2 (R2) {}< br/> void print (); // only non-const objects can be called <br/> void print2 () const; // both the const object and the non-const object can be called. This function will not change R1, R2 <br/>}; <br/> void R: Print () <br/>{ <br/> cout <"non-const:" <R1 <"," <R2 <Endl; <br/>}< br/> void R: print2 () const <br/>{< br/> cout <"const:" <R1 <", "<R2 <Endl; <br/>}< br/> int main () <br/> {<br/> r A (5, 4 ); <br/> // only non-const objects can be called <br/>. print (); <br/> // both the const object and the non-const object can be called <br/>. print2 (); <br/> const r B (20, 52); <br/> B. print2 (); <br/> // error <br/> // B. print (); <br/> return 0; <br/>} 

Result:

Non-const: 5, 4

Const: 5, 4

Const: 20, 52

6 common data members

The Type modifier const not only describes member functions, but also data members.

Because the const type object must be initialized and cannot be updated,When the const data member is described in the class, the const can only be generated through the member initialization list to initialize the data member.

# Include <iostream> <br/> using namespace STD; <br/> Class A <br/>{< br/> PRIVATE: <br/> const int; // constant <br/> const Int & R; // constant reference <br/> static const int B; // static constant <br/> public: <br/> A (int I): A (I), R (A) {}// constant assignment, constant reference assignment <br/> void print (); <br/>}; <br/> // static Constant Value assignment <br/> const int A: B = 10; <br/> void a: Print () <br/>{ <br/> cout <A <"," <B <"," <r <Endl; <br/>}< br/> int main () <br/>{< br/> A A1 (100), A2 (0 ); <br/> a1.print (); <br/> a2.print (); <br/> return 0; <br/>}< br/> 

Result:

100, 10

0, 10, 0

 

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.