C + + Programming-const

Source: Internet
Author: User

1 Use of the const

In C programs, the use of const mainly defines constants, modifier function parameters, and modifier function return values.

In a C + + program, it can also modify the definition body of a function, defining a member function in a class as a constant state function, that is, the data members in the class are not changed.

In the const member function, you can modify the member variables of a class after the member variable name is decorated with mutable.

The const-modified items are protected against accidental changes and can improve the robustness of the program.

2 Extensions

The const in C means "an ordinary constant that cannot be changed." In C, it always takes up memory, and its name is the global character. The C compiler cannot treat Const as a constant during compilation.

Const bufsize;

In C + + it is not right, and the C compiler takes it as a declaration that indicates that there is memory allocation somewhere else.

c The default const is externally connected, and the C + + default const is internally connected.

In C + +, you can use extern to change an internal connection to an external connection.

Eg:extern Const bufsize; Declaration only. can also be used in C.

Using the qualifier const in the C language is not very useful, even in constant expressions (which must be evaluated during compilation) to use a named value, and using const is not very useful. C Forces programmers to use # define in preprocessing.

C + + Programming-const

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.