Global const usage in C ++

Source: Internet
Author: User

C language habits, in C ++, there are some errors. An extern const is used in combination.

Using const directly in C ++ implies the meaning of static. The following program can be compiled in C language. However, in C ++, compilation fails. The Compiler used is mingw GCC or vs2008.

File a. cpp (or a. c)

Const int I = 123; // because the implicit meaning here is static const
...

File main. cpp (or main. c)

Extern const int I;

Int main ()

{

// When I is used here, C ++ will report an error when compiling to the connection process.

}

In C ++, the modification method is:

File a. cpp

Extern const int I = 60; // use extern to remove the implied static meaning of const

...

In C ++, the use of separate const globally implies that the local meaning of the file is static. If you want to use it in other files, the prefix extern is required.


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.