Chapter 02 Talk about static keywords

Source: Internet
Author: User

Quote a little bit.

http://blog.csdn.net/hackbuteer1/article/details/7487694

First, the meaning of static in C language

1, the scope of static variables within a file, the program starts allocating space, the end of the free space, the default is initialized to 0, when used can change its value.

2, static variable or static function only the code in this file can access it, its name is not visible in other files. (Limits the scope of access)

Ii. Extensions in C + +

2.1 Static modifier member variable

If a variable is part of a class, but is not part of the object of the class , it becomes a static statically member. A static member has only one copy, rather than a regular non-static member that has one copy in each object.

A static data member is common to all objects of that class. For multiple objects of this class, static data members are allocated only once for all objects to be shared. Therefore, the value of the static data member is the same for each object, and its value can be updated;

Understand the following:

In the header file, declaring a class (essentially declaring a new data type) does not really allocate your space. It's just a verbal claim that hasn't been put into action.

However, if a member variable of static is declared in the declaration of a class, the static data member is stored in the global data area. A static data member defines a space when it is defined, so it cannot be defined in a class declaration, and since it cannot be defined in a class, it can only be declared in a class, defined outside the class. Examples are as follows:

 class   myclass{ private  :  int   I;             int   J;  static  int  k;//  public  : Myclass ();  int  myclass::k = 100 ; //  Here's the real definition, allocate space in the global data area  

because static data members allocate memory in the global data area, all object shares of this class are shared, so it does not belong to a particular class object, its scope is visible when no class object is produced, that is, we can manipulate it without producing an instance of the class ;

2.2 Static modifier member function

Similarly, a function that requires access to a class member without requiring a call to a particular object is also called a static member function.
A static member function of a class can only access static members (variables or functions) of a class.

An ancestor's killer!

Let's see how Log4cplu's cattle writers use this static modifier's member function:

Because the class is not instantiated, a static member function is used in the form of a class name: Static member function name (argument list).

Chapter 02 Talk about static keywords

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.