Static variables and functions in C/C ++

Source: Internet
Author: User

Let's talk about static variables and static functions in C. (1) static variables in C are stored in the global storage zone, which is different from automatic variables stored in the stack. The key difference between the two is their scope. The scope of the automatic variable is in its function or statement block. The scope of the static variable starts at its definition and ends at the end of the program. (2) The static functions in C mainly restrict the function name and the accessible domain in the file, indicating that this function is only used by this file and cannot be called in other files. The usage of static functions can be considered as follows: first, the names of static functions are only visible to the files defined by them. This allows static functions with the same name to exist in different files. This feature can be used to Abstract Common Code in this file into a function, and directly call the function where this code segment is used. In this way, the function can be defined using a short term. In my experience, it is very difficult to get a beautiful name for functions with the same definition function, and I am still very hard to remember the name. Static functions can save a lot of effort. While normal functions can be accessed globally, and there is no namespace concept in C, so it takes a lot of effort to name functions in C.

In addition, static member variables and static member functions in C ++. (1) static data members of the class are created and initialized at Compilation: they exist before any object of the class is created and do not belong to any object, non-static class member variables belong to the object. A static data member of a class has only one copy, which is shared by all such objects. Note that static data members cannot be initialized in the class (for constant static class variables, it seems that they can be defined before the class or main () function, initialization can be placed in the class). Generally, it is initialized before the class and the main () function. The lack of time is initialized to 0. Static data members are used to define the public data of each object of the class, which is safer than global variables. (2) static member functions of the class belong to the whole class and do not belong to an object, which is shared by all objects of the class. Static members can be defined as inline functions. Generally, static member functions are used to access static data members or global variables in the same class, instead of non-static members. to access non-static members, you need to use objects as parameters, access non-static members of the object through the object name. Static member functions can also be defined outside the class. Static modification is not allowed at this time. Why does a static member function exist? It is mainly used to access static data members before any object is created. Common functions cannot implement this function.

Use of C ++ static members and static member functions: Call format of static members: Class Name: static data member name, Object Name. static data member name, object pointer-> static data member, object reference. static data members (but there are very few public data members in the class. This section only discusses the syntax and does not consider data encapsulation in actual use ). Call format of static member functions: Class Name: static member function name, Object Name. static member function name, object pointer-> static member function name, object reference. static data member. A static member function does not have the this pointer because it is not associated with a specific object. We recommend that you use the "Class Name: static member function name" format for calling. In summary, in the case of objects, static member functions and static member variables can be called by calling common class member functions and common member variables. It can be seen from this that static member variables and static member functions should be called without creating any objects. For its application, see Singleton in design mode.
Pattern.

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.