Static extern const

Source: Internet
Author: User
Static1. local variable
// The life cycle of the variable is not only after entering the function, but also void Foo () {static int nvar = 0; printf ("Var = % d \ n ", ++ nvar );}

2. protect local variables
Static int g_nvar; // g_nvar can only be used in this file. Other files cannot access this variable through extern.

3. static member variables
// This variable does not belong to the object, but belongs to this class Foo {public: static int ncount ;}; int FOO: ncount = 0; int main () {cout <FOO: ncount <Endl; return 0 ;}

4. static member functions
// This function does not belong to an object, but belongs to this class Foo {public: static void showclasstag () ;}; void FOO: showclasstag () {cout <"class foo" <Endl;} int main () {FOO: showclasstag (); Return 0 ;}

Const1. define a constant
const int  nItem = 0;const int* pnItem = &nItem;

2. Protection variables are not used externally
// File1.cconst int g_nlocalvar = 100; int g_nvar = 100; // file2.cextern const int g_nlocalvar; // illegal declaration of extern int g_nvar; // valid

Extern1. the object referenced in the declaration has been defined.
extern int g_nFoo;int main(){g_nFoo++;cout << g_nFoo << endl;return 0;}int g_nFoo = 0;

2. declare that the variable can be used externally
// File1.cextern const int g_nitem; int main () {cout <g_nitem <Endl; return 0 ;}// file2.c // If extern is not added here, extern const int g_nitem = 100;


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.