Usage of C + + const

Source: Internet
Author: User

1. Modify member variables

int value=0;int *p=&value;const int *p_c=&value; the// pointer points to a constant , but the address pointed to by the pointer can be modified (int const *p_c=&value, also pointer to constant)int *const  c_p=& Value The //(constant) pointer points to the variable , but the address pointed to by the pointer is constant and cannot be changed

2, modifier function parameters (often and references at the same time, improve the efficiency of the transfer, and make the parameters can not be changed by the algorithm in the function body).

3. Modify the member function.

Note: 1. You cannot modify member variables (unless you add the mutable keyword);  2. Non-const member functions cannot be called (normal member functions can modify member variables)

4, modify the ordinary variable (directly as a constant use, in place of the macro).

Usage of C + + const

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.