5. Static members of a class

Source: Internet
Author: User

Question: How do you count the number of objects?

(1) Common member variables? No, because for normal member variables, objects have their own member variables, and they are independent of each other, so it is not possible to count the number of objects

(2) Global variables: Yes, but generally speaking, we try to avoid global variables (long periods, memory, error, difficult to find),

(3) Static member variables and static member functions for a class: YES

Static Members:

(1) A static member is owned by the entire class, does not depend on any object, depends on the existence of the class (so only static member variables and static member functions can be accessed directly through the class name), and object creation and destruction do not affect static members

(2) Static members of public can be accessed through the class name (ordinary member functions cannot be accessed through the class name, only static member functions can be accessed by the class name)

(3) Static members of public can be accessed by object name

(4) A static member is not assigned within a class because it is shared by all objects of that class and is typically initialized outside the class

(5) Static member functions can only access static member variables and static member functions directly, not static member variables and non-static member functions (rather than static member functions can access all static and non-static, that is, can access static member variables and static member functions, More access to non-static member variables and static member functions)

Static member variables:

In fact, simply add the static keyword to the inside of the class variable declaration, static member variables do not depend on any object, you need to allocate space outside the class, in fact, is stored in the Global data area (static storage area).

class test{public:    Test ();     ~Test (); Private :     Static int b;  // }; int 0;  // initialize static member variables outside of the class

The reason why static member variables are initialized in a class is because, in the internal initialization of the class, it is shared by all objects of the class, which is not what we want; therefore, the static member variable of the generic class is the static member variable initialization outside the class.

It can be understood that a static member of a class is a global variable inside a namespace within a class, that is, a static member variable is a global variable within a class.

Static member functions:

is the function name plus the static keyword

classa{Private:    Static intI//static member variables, adding the static keyword Public:    Static intGeti ()//static member function, static keyword    {        returni; }    Static voidSetiintv) {i=v; }};//do not initialize within a class because static member variables are common to all objects .intA::I =Ten;//static member variables are initializedintMain () {A AA; Aa.seti (Ten);//static member variables are private, so they can only be accessed by functions within the classprintf"i =%d\n", Aa.geti ());//Access by Objectprintf"i =%d\n", A::geti ());//access by class name     while(1);}

Attention:

Static member functions can (only) directly access static member variables and static member functions within the class (Cannot access non-static member variables and non-static member functions), but cannot directly refer to non-static member variables and non-static member functions, which can be an error. If a reference is not to be made, unless the object name is obtained by means of a parameter, the object's access is achieved.

Statistics on the number of objects implemented:

 class   a{ private  :  static    int  count; //  static member variable  public  : A ()  //  constructor      {count  ++; }  ~a () //  destructor   {count --; }}; int   A::count = 0 ; //  Static member variables are initialized  

Each time the object of the class is created, count will add one, and when the object is destroyed, it will subtract one, so that the implementation of the number of objects is completed (non-static member functions can access static member functions and static member variables);

Attention:

Static member variables are initialized, usually in the outside of the class, if not their own initialization, is actually the value static member variable default initial value is zero, cause analysis: Because the static member variable is stored in the static storage area (Global storage area) of all the non-initialized values, The compiler automatically assigns them a value of zero.

The difference between a static member function and a normal function

(1) The difference between the static keywords,

(2) A static member function is actually a global function within a class, does not depend on the object, but relies on the class, while the normal member function is dependent on an object within the class.

(3) Static member functions can access only static member variables and member functions in a class, and ordinary member functions are accessible to all ordinary member functions and member variables in the class

(4) A static member function does not contain a pointer to a specific object (there is no this pointer), and the normal member function contains a pointer to a specific object,

Attention:

The this pointer is used by the compiler to automatically pass the address of the object itself to a function within the class, passed to this. This pointer is scoped to the inside of the class, remember that the this pointer is only in the ordinary member function of the class, the static member function is absolutely no this pointer,

Allocation of storage space:

classa{Private:    inti; intJ;  ShortA;  Shortb;};classB {Private:    inti; intJ;  ShortA;  Shortb; Static intc;};classc{Private:    inti; intJ;  ShortA;  Shortb; Static intC//Global Data area (static store) Public: C ()//part of the function is the stack space    {    }    ~C () {}};intC::c =Ten;

Three classes, the size of the allocated space, is 12 bytes, because the static member variable is not allocated within the object, but is allocated in the global data area (static storage area), and the function whether it is static or not is in the code snippet.

The memory distribution of a member variable within a class is similar to that of a struct, and is also required for memory alignment, which can be used to learn the alignment of a struct.

5. Static members of a class

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.