C + + CONST Keyword Summary

Source: Internet
Author: User

1. The const modifier variable, which indicates that the value of the variable does not change:

Const TYPE ValueName = value;

2. The pointer uses the const

(1) The pointer itself is constant, with the following two types of notation:

(char*) const pName;

(2) The pointer points to the content is constant, there are the following two ways:

(char) const *pname;

Const (char) *pname;

(3) both are not changed

const char* Const pName;

3. const modifier Function parameters

Parameters cannot be changed within a function: void function( const intVar);

The content pointed to by the parameter pointer cannot be changed within the function: void function( constchar* Var);

The parameter pointer itself is a constant that cannot be changed: void function( char * constVar);

Parameters are references that increase efficiency while preventing modification: void function( constTYPE& Var);

4. const modifier function return value

const int* fun2(),调用时const int *pValue = fun2();//我们可以把fun2()看作成一个变量,即指针内容不可变

  int * const fun3(), 调用时int * const pValue = fun2();We can think of fun2 () as a variable, that is, the pointer itself is not mutable

C + + CONST Keyword Summary

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.