C + + static member variables and static member functions

Source: Internet
Author: User
Tags static class

static variables and static functions in C.
(1) The static variables in C are in the global store, which differs from the automatic variables stored in the stack. The key difference between the two is the difference in scope. The scope of an automatic variable is in the function or statement block in which it resides, and the scope of the static variable begins at its definition and terminates at the end of the program.

(2) A static function in C is primarily a restriction on the name of a function and an accessible domain in a file, which means that this function is used only for this file and is not allowed in other files. There are several things to consider about the use of static functions: First, the name of a static function is visible only to the file that it defines. This allows static functions with the same name to be present in different files. You can use this attribute to abstract the code commonly used in this file as a function, and call the function directly where you use this code. This allows you to define this function in a short term. My experience, in the face of defining functions of the same function is, for them to take a beautiful name is actually very difficult, and in the use of the name of the memory is also quite painstaking. and static functions can save a lot of effort. Ordinary functions can be accessed globally, and there is no concept of namespace in C, so the name of the function in C is rather painstaking.


static member variables and static member functions in C + +.
(1) class static data members are created and initialized at compile time: they exist before any object of the class is established, are not part of any object, and Non-static class member variables are owned by the object. Class static data members have only one copy, which is shared by all objects of this class. It is to be noted that: static data members cannot be initialized in a class (for constant static class variables to be verified, as if they could be defined outside of the class or before the main () function, initialization can be placed in a class), typically initialized before the class and the main () function, and initialized to 0 by default. Static data that members use to define the objects of a class are more secure than global variables. (2) class static member functions belong to the entire class and are not part of an object that is shared by all objects of that class. Static members can be defined as inline functions. In general, static member functions are used to access static data members or global variables in the same class, but not to non-static members, and if you want to access non-static members, you need to use the object as a parameter to access the object's Non-static members through the object name. Static member functions can also be defined outside of a class and cannot be decorated with static. What is the reason for the existence of static member functions? The main thing is that you can use it to access static data members before any object is established, and normal functions do not implement this functionality.

Use of C + + static members and static member functions: Call format for 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 is rarely a public data member in a class, this is just a discussion of syntax, does not consider the actual application of data encapsulation problem). Call format for 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 members. Static member functions do not have the this pointer because it is not associated with a particular object, and it is recommended to use the "class name:: Static member function name" format when invoked. In summary, static member functions and static member variables can be invoked in a way that calls ordinary class member functions, ordinary member variables, in the case of objects. It can be seen from here that the use of static member variables and static member functions should be invoked without creating any objects. The application can see Singleton pattern in design mode.

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.