What do static class members do

Updated on healthy 2024-02-08
15 answers
  1. Anonymous users2024-02-05

    Static --- static.

    Here's an example. class t

  2. Anonymous users2024-02-04

    Originally, the things that come out of new exist in the heap, that is, if you new an object, its data members are generated once in the heap, and the member functions are shared. If you declare a data member as static, the data member is stored in the data area, and this one is shared no matter how many times you update it. Therefore, the sharing of data is realized.

    For example, there is a student class with an int static allstudent=0;You can implement allstudent++ in the constructor; This way, every time a new student object is added, the value of allstudent will be changed to add 1. Count how many students there are.

  3. Anonymous users2024-02-03

    static members.

    Categorize owned, keep only one copy in memory, and share it with multiple objects.

    For ordinary member variables, creating an object will produce one, and static member variables are for multiple objects to share this variable, and all of them can change its value.

  4. Anonymous users2024-02-02

    Static members are loaded when the class is loaded, only once, and are properties that are shared by all class objects and can be accessed directly by static methods.

  5. Anonymous users2024-02-01

    Static methods can be called directly without objects

    String class has a static method that is valueof(). You can write valueof("a") to get the value of the string; But like int class has a method called tostring(), which is not static, so we need an object when we call int a= 0; ;

  6. Anonymous users2024-01-31

    That is, it is global and can be called arbitrarily, but its internal functions are called by static-modified methods.

  7. Anonymous users2024-01-30

    Static variables in C can be referred to as static local variables. In general, the scope and lifetime of a variable defined within a function is limited to that function. But if static is added in front of it, his life cycle will increase.

    That is to say, after leaving this function, this static variable will not be **, it will always exist, and it will maintain the value of the last time, which is similar to the nature of a global variable. However, unlike global variables, static local variables are still limited to this function. Hope it helps!

  8. Anonymous users2024-01-29

    2. Static modified variables.

    1.Global static variables.

    By adding the keyword static before a global variable, the global variable is defined as a global static variable.

    1) Location in memory: Static storage (static storage exists for the entire duration of the program).

    2) Initialization: Uninitialized global static variables are automatically initialized to 0 by the program (the value of the automatic object is arbitrary, unless it is shown to be initialized).

    3) Scope: A global static variable is not visible outside of the file in which it was declared. Precisely, starting at the point of definition and ending with the document.

    Benefits of Defining Global Static Variables:

    1> will not be accessed or modified by other files.

    2> Variables with the same name can be used in other files without conflict.

    2.Local static variables.

    By adding the keyword static before the local variable, the local variable is defined as a local static variable.

    1) Location in memory: Static storage.

    2) Initialization: Uninitialized local static variables will be automatically initialized to 0 by the program (the value of the automatic object is arbitrary, unless it is displayed initialized).

    3) Scope: The scope is still a local scope, and when the function or statement block that defines it ends, the scope ends with it.

    Note: When static is used to modify local variables, it changes the storage location of local variables, from the original stack storage to static storage. However, the local static variable is not destroyed after leaving the scope, but remains in memory until the program ends, but we can no longer access it.

  9. Anonymous users2024-01-28

    A static member of a "class" in C++ with only one instance. This means that all instances of the class use the same variable. If the variable is public, the outside of the class can be accessed directly by the class name "variable name".

    Instance variables, class variables, and constants in ** all belong to member variables, which are related to the modifiers of variables, that is, private, static, final, and other modifiers in ** above.

    First of all, the static member variables of the class are global variables of the class, and are not owned by the objects of the class. However, the ordinary member variables of a class are unique to the generated object after the object is generated by the class, and cannot be shared with other generated objects. As shown in the figure below, the member variables x of the two objects A and B are not identical.

    Static member variables need to be defined globally, while ordinary member variables belong to an object and do not need to be defined globally. The static member variables of the class can be accessed in the following form, such as the static member variable x defined in class A, in the form of class name, variable name, and the static member variables of the class exist before the object is generated.

  10. Anonymous users2024-01-27

    The static member function, although it is a "member" function, is not really a member, but a global function, but the scope is constrained by the class (i.e., public can only be used as a global, and non-public can only be used in objects of this class).

    Since it's similar to a global function, it has nothing to do with objects.

    You want to use a certain class of member variables in the static member function. You first have to make sure that there is a certain kind of "object", no"Object"There are no member variables for the object. Whereas, static member functions are global functions and do not depend on"Object", i.e. does not need to be created out"Object"It is also possible to call the static member function.

    So when you call the static member function, the function belongs"Object"is non-existent (static has no object to belong to at all). Therefore, the "class" object of the phrase "using the member variables of the class" does not exist.

    The static function does not have an object, so it cannot use the object's member variables. However, it can modify the member variables of other objects.

    You can create one"Object"and put it again"Object"As a parameter, pass it to the static function, and then modify the member variables inside the object parameter in the static function.

  11. Anonymous users2024-01-26

    After the class is declared, just initialize it directly, write an example.

    class a

    int data;

    public:

    static a *p;

    static void fun();

    a* a::p = new a(100);

    void a::fun()

    int main()

  12. Anonymous users2024-01-25

    Create one"Object"and put it again"Object"As a parameter, it is passed to the static function, and then the member variables inside the object parameter are modified in the static function.

  13. Anonymous users2024-01-24

    No, but you can pass a parameter to manipulate it.

  14. Anonymous users2024-01-23

    A: In C language, static mainly defines global static variables, defines local static variables, and defines static functions.

    1. Define a global static variable: Add the keyword static in front of the global variable, and the global variable becomes a global static variable. Global static variables have the following characteristics:

    1) Allocate memory within the global datazone.

    2) If it is not initialized, its default value is 0

    3) This variable is visible in this file from the beginning of the definition to the end of the file.

    2. Define local static variables: Add the keyword static in front of the local static variable, and the local variable becomes a static local variable. Static local variables have the following characteristics:

    1) This variable allocates memory in the global data zone.

    2) If initialization is not shown, then it will be implicitly initialized to 0

    3) It always resides in the global datazone until the end of the program run.

    4) Its scope is local, and when the block of functions or statements that define it ends, its scope ends with it.

    3. Define a static function: Add the static keyword to the return type of the function, and the function is defined as a static function. Static functions have the following characteristics:

    1) Static functions can only be used in this source file.

    2) The inline function declared in the scope of the file defaults to static

    Note: The static function is just an ordinary global function, but due to static restrictions, it can only be used in the compilation unit where the file sits, and cannot be used in other compilation units.

  15. Anonymous users2024-01-22

    There are two main uses.

    Let a variable be valid for a long time, regardless of where it is stated. For example:

    int fun1()

    Regardless of where fun1 is called, the last value of s value will always be saved by the system (equivalent to a global variable) after the function exits, and the next time s value is used again, that is, when fun1() is called again, the initial value of s value will be the most recently saved value (note that the s value initialization operation will only be performed once, i.e. the above statement s value = 0).

    2.Avoid conflicts caused by multiple files using the same variable name, such as multiple files developed by several people independently. Assuming they define the same "global" variable name in their own file (meaning only global in their own file), when the system is integrated, they use the same "global" variable, which leads to difficult problems.

    A convenient way to solve this problem is to add a static modifier to the same global variable declaration in each file. This way, the system allocates different memories to them, regardless of each other.

Related questions
13 answers2024-02-08

Crystal Romance - Mouse Auto Clicker.

File size: 727 KB >>>More

8 answers2024-02-08

What are the benefits of egg whites? Egg whites are semi-flowing gelatinous substances in the skin under the shell, mainly ovalbumin. Egg whites contain a certain amount of riboflavin, niacin, biotin, calcium, phosphorus, iron, and other substances. It has the effects of moisturizing the lungs, clearing heat and detoxifying, and supplementing nutrition. >>>More

7 answers2024-02-08

It's honey that's good and can nourish the stomach.

12 answers2024-02-08

Through the computer's task manager, you can view the details of the computer's CPU and memory, and then I will tell you how to check the computer's CPU usage. >>>More

10 answers2024-02-08

Rotting will affect the lighting effect.

After installing a solar cell on the edge of the glass, a hybrid coating is applied to the surface of the glass, and the paint absorbs sunlight and transmits the light to the solar cell mounted on the edge of the glass at different wavelengths. Mark Baldo, an electrical engineer at the Massachusetts Institute of Technology and head of the research group, said: "Coatings can capture sunlight on large objects, such as glass windows. >>>More