Va 147 Dollars (full backpack)

Source: Internet
Author: User

The role of the const qualifier: 1. Define a const constant: const can change an object into a constant and cannot be modified. Therefore, Initialization is required during definition, for example, const int bufsize = 512; 2. You can modify the parameters, return values, and even definition bodies of a function. Forced protection of things modified by const can prevent unexpected changes and improve program robustness. A non-const variable is defined in the general global scope, which can be accessed throughout the program; // file_1.ccint counter; // defines a non-const variable // file_2.ccextern int counter; // use the counter variable + + counter in the file_1.cc file. If the const variable is declared in the global scope, it is a local variable that defines the object and only exists in that file, cannot be accessed by other files; you can specify the const variable as extern to access the const object throughout the program // file_1.ccextern const int counter = fcn (); // define the const variable and specify it as extern. counter can be used as the global variable // file_2.ccextern const int counter; // use the counter variable + + counter; = in the file_1.cc File ========================================================== ========================================================== ============== Const and # define are different: the C ++ language can use const to define constants or # define to define constants, but the former has more advantages than the latter: 1. const constants have data types, however, macro constants do not have data types, so the compiler can perform type security checks on the former and only replace the latter. 2. Some integrated debugging tools can debug const constants, however, macro constants cannot be debugged. ========================================================== ========================================================== ==================== In C ++, the member variable cannot be modified within the function body modified by const. However, in some cases, you need to change the member variables in the const function. This requires the member variable to be set to the mutable type, for example, class C {public: C (int I): m_Count (I) {} int incr () const // here, the member function incr is specified as the const type, so the variables in the function cannot be modified, that is, the m_Count variable cannot be changed. If m_Count can be changed, convert m_Count to mutable {return ++ m_Count;} private: mutable int m_Count; // here}

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.