The static in C + +

Source: Internet
Author: User

First, static global variables

Before you understand the static keyword, first review the in-memory allocations for C + + programs. from low address to high address is divided into: Code area, global data area, heap area, stack area. global variables and static variables outside the function (including global variables and static variables) are stored in the global data area, and the heap has the programmer to request memory through Malloc,new, which stores temporary variables inside the function, freeing the memory as the function exits.

The difference between a static global variable and a normal global variable:

    • Both the static global variable and the normal global variable are stored in the global variable area, at which point the two are the same.
    • The scope of a normal global variable is the entire project, the global variable defined in a file, when referenced by other files, at the beginning with extern marked with the external variables used. The scope of a static global variable is the file that defines the variable and cannot be referenced in an external file.

For example: When n is a global variable that can pass normally, when n is defined as a static global variable, it cannot be compiled by

//Example2//File1 code for the first code file#include <iostream>#include"example2.h"intN//defining static global variablesvoidMain () {n= -; Std::cout<<n<<std::endl;// -FN ();// +}//File2 code for the second code file#include <iostream.h>extern intN;voidfn () {n++; Std::cout<< N <<Endl;}
Second, static local variables

We know that local variables defined inside the function are stored in the stack, and the system reclaims the stack memory as the program exits the function, and the local variables are invalidated. There are times when you need to store local variables in the global data area by introducing static local variables. Static local face-changing only at the first initialization, each time the value is saved to the next call, always resides in the global data area until the program exits. The scope of the static global variable is not changed and is still local.

#include <iostream>usingnamespace  std; void fn (); void Main () {    fn ();    fn ();    fn ();} void fn () {    staticint n=;     << n << Endl;    N+ +;}
Third, static function

With regard to the practice of limiting function scopes to static functions, I have experimented with C + + and found that it has not worked. On the other hand, it is seldom used, but not much. In C + + object-oriented, the key is static member, static member function.

Iv. Static member variables
    • For non-static data members, each class object has its own copy. static data members are treated as members of the class . Regardless of how many objects of this class are defined, a static data member has only one copy in the program, shared by all objects of that type, including objects of its derived class . Therefore, the value of the static data member is the same for each object, and its value can be updated;
    • static data members are stored in the global data area. When a static data member is defined, it is allocated space, so it must be declared in the class, outside of the class definition. For example, in the example below, int myclass::sum=0; is a static data member defined ;
    • static data members comply with public,protected,private access rules like normal data members; if a static data member defines access permission, it is defined as public; the < class object name >.< static data member name > or < class type name >::< static data member name > access to a publicly-owned static member
There are two advantages to using static data members compared to global variables:
    1. Static data members do not enter the program's global namespace, so there is no possibility of conflicts with other global names in the program;
    2. Information hiding can be implemented. A static data member can be a private member, while a global variable cannot;
1#include <iostream>2 using namespacestd;3 classMyclass {4  Public:5Myclass (intAintBintc);6     Static voidGetsum ();//declaring a static member function7 Private:8     intA, B, C;9     Static intSum;//declaring a static data memberTen  One }; A intMyclass::sum =0;//defining and initializing static data members -  -Myclass::myclass (intAintBintc) { the      This->a =A; -      This->b =b; -      This->c =C; -Sum + = a + B + C;//non-static member functions can access static data members + } - voidMyclass::getsum () {//static member function implementation +     //cout << a << Endl; //error,a non-static members Acout <<"sum="<< Sum <<Endl; at } - intMain () { -cout <<sizeof(Myclass) << Endl;//static members do not occupy object space -Myclass M (1,2,3); - m.getsum (); -Myclass::getsum ();//static member function Access inMyclass N (4,5,6); - n.getsum (); to m.getsum (); +     return 0; -}
V. Static member functions
    • As with static member variables, you can also define static member functions. A static member function serves all objects of a class, not a specific object for a class
    • static member functions are declared in a class and can be used outside of class, and cannot be added to the static keyword when implemented outside of the class. A static member must be declared in a class, out-of-class initialization
    • Static members can access each other, including static member functions that access static data members and access static member functions, and non-static member functions can arbitrarily access static member functions and static data members; Static member functions cannot access non-static member functions and non-static data members;
    • Non-static members and non-static member functions cannot be accessed because the static member function does not have the this pointer
    • Access to a static member function can use the class's access method < class name:: member function > or how object members are accessed < objects. member functions >, < object pointers, member functions >
    • Static member functions cannot be declared as virtual, const, volatile functions at the same time

The static in C + +

Related Article

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.