Static summary in C + +

Source: Internet
Author: User

Content interception from http://blog.csdn.net/hackbuteer1/article/details/7487694

The C + + keyword Static is used in two ways: in process-oriented programming (common variables and functions in C) and in object- oriented programming (classes in C + +).

static variables and static functions in process-oriented programming

Distribution of program in-memory:

Code Area

Global Data area

Heap Area

Stack area

    • Code Area Storage: code.
    • Global Data areas are stored: global static variables and local static variables.
    • Heap Storage: Dynamic Data generated by new from the general program.
    • Stack Store: An automatic variable inside a function. Released as the function exits.

1. Static Global variables

See the following example:

 #include <iostream>using  namespace   Std;  static   int  N; //  defines a static global variable  void      FN () {n  ++ <<n<<ENDL;}  int  Main (void  ) {n  = 20      ;    cout  <<n<<ENDL;    FN ();  return  0  ;}  

Characteristics of static global variables:

    • This variable allocates memory in the global data area
    • Uninitialized static global variables are automatically initialized by the program to 0 (the value of the automatic variable is random unless it is explicitly initialized)
    • A static global variable is visible throughout the file that declares it, and is invisible outside the file, i.e. it cannot be referenced by another file
    • Variables of the same name can be defined in other files, and no conflicts will occur (if the global non-static variable is a conflict)

2. Static local Variables

Example:

 #include <iostream>using  namespace   Std;  void   FN ();  int  Main (void      return  0  ;}  void   FN () { static  int  n = 10      <<n<<ENDL; n  ++;}  

Normal Local variables : Each time the function is called, the system allocates the stack memory to the local variable. As the function exits, the stack memory is recycled and the variable is invalidated.

static local variables : stored in the global data area, allocated only once memory, each call can update the value of the change, can only be used in local areas (compared to static global variables).

Characteristics of static local variables

    • The variable allocates memory in the global data area;
    • A static local variable is first initialized when the program executes to the declaration of the object, that is, the subsequent function call is no longer initialized;
    • Static local variables are typically initialized at the declaration and, if not explicitly initialized, are automatically initialized to 0 by the program;
    • It always resides in the global data area until the program finishes running. But its scope is local scope, when the function or statement block that defines it ends, its scope ends;

3. Static functions

 #include <iostream>using  namespace   Std;  static    void  fn (); //  declares a static function  int  Main (void      return  0  ;}  void  fn () //  defines a static function  { int  n =  ; cout  <<n<<endl;}  

static function Features:

    • Static functions cannot be used by other files;
    • Other files can define a function of the same name, and no conflict will occur;

static data member (Class) and static member function in object-oriented programming

1. Static data members

#include <iostream>using namespacestd;classmyclass{Private:    intA, B, C; Static intSum//declaring a static data member Public: Myclass (intAintBintc); voidgetsum ();};intMyclass::sum =0;//defining and initializing static data membersMyclass::myclass (intAintBintc) {     This->a =A;  This->b =b;  This->c =C; Sum+ = a+b+C;}voidmyclass::getsum () {cout<<"sum="<<sum<<Endl;}intMainvoid) {Myclass M (1,2,3);    M.getsum (); Myclass N (4,5,6);    N.getsum ();    M.getsum (); return 0;}

Static data member Features:

    • As a member of a class, no matter how many objects there are, there is only one copy, saving space. all objects of the class are accessible and can be used without generating an instance of the class.
    • stored in the global data area, allocated space when defined, cannot be defined in the class declaration (defined outside the class), defined (initialized) format: Data type >< class name >::< static data member name >=< value
    • Access static data members of a class in two formats: Class object name >.< static data member name or class type name >::< static data member name > (the latter requires static data members to be public members)
    • For each object that has the same properties, such as a deposit class, where interest is the same for each instance, interest can be set to a static data member.

Static data members compared to global variables:

    • Does not conflict with other identical names in the global namespace
    • Implement information hiding. can be private.

2. Static member functions

is an internal implementation of a class that is part of a class definition. is not part of a class object, but belongs to all objects of the class.

  The normal member function implies the this pointer (the default), which points to the class object itself, such as the function func (), which is actually this->func ().

  A static member function does not contain the this pointer because it is not related to any object. You cannot access non-static data members and non-static member functions of Class objects, only static member functions can be called.

Example:

#include <iostream>using namespacestd;classmyclass{Private:    intA, B, C; Static intSum//declaring a static data member Public: Myclass (intAintBintc); Static voidGetsum ();//declaring a static member function};intMyclass::sum =0;//defining and initializing static data membersMyclass::myclass (intAintBintc) {     This->a =A;  This->b =b;  This->c =C; Sum+ = A+b+c;//non-static member functions can access static data members}voidMyclass::getsum ()//implementation of static member functions{    //cout<<a<<endl; //error code, a non-static data membercout<<"sum="<<sum<<Endl;}intMainvoid) {Myclass M (1,2,3);    M.getsum (); Myclass N (4,5,6);    N.getsum ();    Myclass::getsum (); return 0;}

Static member function Features:

    • Static member functions can access static data members and static member functions, where static members can access each other
    • Static member functions do not have access to non-static member functions and non-static data members
    • Non-static member functions can access both non-static data members and static data members and static member functions
    • Calling a static member function of a class: class name >::< static member function name > (parameter table)
    • Call the static member function of the object: class object name >.< static member function name > (parameter table)

Compared to global functions, static member functions:

  Faster access because there is no additional overhead for this pointer

Static summary 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.