"C + +" static explanation

Source: Internet
Author: User

    • The role of Static

   1. Hide

When we compile multiple files, all global variables and functions that do not have a static prefix are globally visible and other source files can be accessed. For example, we have the source file Source1.cpp defines a global variable i and function func

 1    source1.cpp  2  int  i = 0   3  void   Func ()  5  { 6  printf (  " hello world\n   "  7 }  

If I and Func can be referenced in another file main.cpp. However, if a file is to use a variable or function defined in another source file, be sure to include the declaration of the corresponding variable or function in this file. The variables must be declared with extern, and the declaration of the function is not affected by the addition of extern. is as follows:

1 //Source1.cpp2 extern intI//declaration of the variable i3 voidFunc ();//declaration of a function4 5 intMain ()6 {7printf"%d\n", i);//using the variable i8Func ();//use function func ()9 Ten     return 0; One}

If the static prefix is added, it cannot be used by other programs like the program above.

1 Static int 0 ; 2 3 Static void Func () 4 {5     printf ("Hello world\n"); 6 }

2. The second effect of static is initialized to 0 by default, including uninitialized global variables and local static variables. In fact, the uninitialized global variable also has this property because the uninitialized global variable is stored in the same area as the uninitialized static variable (BSS segment)

3. The third function of static is to keep the contents of local variables persistent.

The automatic variable inside the function disappears when the function is called, but while the static local variable is defined within the function, the static local variable is always present, that is, its declaration period is the whole source program, which is characterized by only one initialization and the rejection of ' memory '.

The lifetime of a static local variable is the entire source program, but its scope is still the same as the local variable-that is, the variable can only be used within the function that defines it. After exiting the function, you cannot continue to use it even though the variable continues to exist.

  • The role of static in a class

    Static data members:
    C + + reuses the keyword static and gives it a different meaning: variables and functions that represent any particular object belonging to a class other than such an object.
    Typically, non-static data members exist in each object of the class type. static member variables always have only one instance, regardless of how many objects the class produces, and already exist without an object instance. Unlike normal data members, static data members exist independent of any object of the class, and each static data member is an object associated with the class and is not associated with an object of that class. This means that when an instance of a class modifies the static data member variable, its modified value is seen by all other instances of the class.
    data members conform to public, protected, private access rules as normal and normal data members.
    A static member can be called directly from a class by a scope operator, or indirectly by an object, reference, or pointer to an object of that class type.
    the data members of the class are also stored in the global (static) store. Static data members are defined when they are allocated space, so they cannot be defined in a class declaration.
    static data members can be declared as arbitrary types, which can be constants, references, arrays, class types, and so on.
    static data members must be defined outside the class definition body (exactly once), unlike normal data members, which are not initialized by the class constructor, but should be initialized at the time of definition.
    an exception to this rule is that, as long as the initialization is a constant expression , the const static data member can be initialized (not defined)in the class's definition body. However, a const static data member must be defined outside the definition body of the class.
          
    The best way to ensure that an object is precisely defined is to place the definition of a static data member in a file that contains a non-inline member function definition for the class.
    Double account::interestrate = initrate ();
    As with any class member, when you reference a static member of a class outside the body of a class definition, you must specify which class the member is defined in. However, the static keyword can only be used in declarations within the class definition body, and the definition cannot be marked as static.
    A static member of a class can be accessed by a static method of a class, or it can be accessed by a non-static method.


    Static member functions:
      
    A static member function, like a static data member, is an internal implementation of a class that is part of a class definition and serves a class service rather than a specific object of a class.
    Because ordinary member functions are always specific to a specific object of a class, ordinary member functions generally imply a this pointer, which points to the object itself of the class.
    However, a static member function does not have the this pointer, as compared to a normal member function, because it is not associated with any object. As a result, it cannot access non-static data members that belong to class objects, nor can it access non-static member functions, which can only invoke the rest of the static member functions with access to static data members.
    Because the static member is not part of any object, the static member function cannot be declared as Const. Also cannot be declared as virtual function, volatile.
    A static member function does not need to be called through a class object, or it can use a class name, a pointer to a class object, or a reference call like a static data member:
    1 classA {2  Public:3     Static voidFunc () {4cout<<"Hello World"<<Endl;5     }6 7     Static inti;8 };9 Ten intA::I =9; One  A intMain () - { - A; theA * PA = &A; -A &ra =A; -  -A::func ();//calling a static member function with a type +A.func ();//invoking a static member function with an object -Pa->func ();//invoking a static member function with an object pointer +Ra. Func ();//calling a static member function using an object reference A  atcout<<a::i<<endl;//calling a static data member using a type -cout<<a.i<<endl;//invoking a static data member using an object -cout<<pa->i<<endl;//To invoke a static data member using an object pointer -cout<<ra.i<<endl;//calling static member data using object references -  -     return 0; in}


  • Resources
    1. "C + + Primer fourth Edition"
    2. "Benevolent Programmer"
    3. Deep Exploration of the C + + object model

"C + +" static explanation

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.