The usage of the const modifier in C ++ is described in detail!

Source: Internet
Author: User
• Const: constant modifier, which converts an object to a constant (constant ). The const object must be initialized and defined at the same time. The const object (or pointer) after initialization cannot be modified.

Example 1:

Int I = 0; const Int J = 0; // int const J = 0; const int * P; // int const * P; change P but not * pint * const P = & I; // change * P but not change P const int * const P = & I; // int const * const P = & I; P, * P cannot be changed

Example 2: Why is the error correct? Think for yourself

Class test {public: const int I; // correct Int & J; // correct}; int main () {const int I; // compilation error: uninitialized const 'I' Int & J; // compilation error}

Initialization:

Generally, the const object must be initialized and defined at the same time.

If it is defined in a class, that is, a regular data member, it must be initialized in the initialization list!

class Test{public:const int i;int & j;Test(int a,int b):i(a),j(b){}};

• Const modifier pointer: If a variable has been declared as a constant variable, it can only be pointed to by a pointer variable pointing to the constant variable.
Int main () {const char C [] = "boy"; const char * P1; P1 = C; char * P2 = C; // error! Error: Invalid conversion from 'const char * 'to 'Char *' Return 0 ;}



• Const modifier member functions: const class objects can only call const member functions, while const member functions cannot modify member variables of classes. To modify a member variable, the const member function must use mutable to modify the member variable.

Class test {public: int I; void F () const {// error! I ++ ;}};

 error: increment of member 'Test::i' in read-only object

Example 2:

class Test{public:int i;int get() const{return i;}void set(int a){i = a;}};int main(){const Test t; // error: uninitialized const 't'}

Int main () {test T1; const test t2 = T1; t2.get (); // correct. t2.set () cannot be called. if you remove the const modifier of the get () method, this sentence is also incorrect !}

• Const modifier function parameters: If a parameter is passed by value, do not use const modifier because it does not work and is easy to mislead. It is best to change the function parameter passed by value to the reference parameter of Const.

class Test{public:int i;int get() const{return i;}void set(const int a){i = a;a++; // error: increment of read-only parameter 'a'}};

Int N; static int _ n; void F (Int = N); // Error !!! Void F2 (Int = _ n );

: error C2648: 'n' : use of member as default parameter requires static member

• Const modifier function return value: When the function return value is not an internal type, the const is usually used to modify the return value. Because the function return value is a temporary object, this helps the compiler check whether the caller attempts to modify this temporary object.

• Const aims to ensure correctness without running overhead (completed during compilation ). In C ++, the correctness of const is only another form of type information. It is to language the correct information in the programmer's mind.

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.