Static functions and usage in C ++

Source: Internet
Author: User

1. What is static?
Static is a common modifier in C ++. It is used to control the storage and visibility of variables.

2. Why is static introduced?
When a variable defined in a function is executed to its definition, the compiler allocates space for it on the stack, the space allocated by the function on the stack is released at the end of the function execution. This creates a problem: If you want to save the value of this variable in the function to the next call, how to implement it? The easiest way to think of is to define a global variable, but defining a global variable has many disadvantages, the most obvious drawback is that the access range of the variable is broken (so that the variables defined in this function are not controlled by this function ).

3. When should I use static?
A Data Object is required to serve the entire class rather than a specific object, and the encapsulation of the class is not damaged. That is, the member is required to be hidden inside the class and invisible to the outside world.

4. static internal mechanism:
Static data members must exist at the beginning of the program. Because the function is called during the running of the program, static data members cannot allocate space and initialize it in any function.
In this way, there are three possibilities for its space allocation. One is the header file of the class's external interface, where there is a class declaration; the other is the internal implementation of the class definition, there is a member function definition for the class, and the third is the global data description and definition before the main () function of the application.
Static data members must actually allocate space, so they cannot be defined in the class declaration (only data members can be declared ). The class declaration only declares the "size and specification" of a class and does not actually allocate memory. Therefore, it is wrong to write a definition in the class declaration. It cannot be an external definition of the class declaration in the header file, because it will duplicate the definition in multiple source files that use the class.
Static is introduced to inform the compiler that the variables are stored in the static storage area of the program rather than the stack space.
Data members are initialized sequentially according to the sequence in which they appear. Note that when static members are nested, ensure that the nested members have already been initialized. The order of cancellation is the reverse order of initialization.

5. static advantages:
Memory can be saved because it is public to all objects. Therefore, for multiple objects, static data members only store one object for sharing. The value of a static data member is the same for each object, but its value can be updated. You only need to update the value of the static data member once to ensure that all objects have the same value after the update, which improves the time efficiency.

6. When referencing static data members, use the following format:
<Class name >:: <static member name>
If the access permission of the static data member is allowed (that is, the public Member), in the program, according to the above format
To reference static data members.

7. Notes:
(1) The static member function of the class belongs to the entire class rather than the class object, so it does not have the this pointer, which leads
It only supports the static data and static member functions of the category.
(2) static member functions cannot be defined as virtual functions.
(3) Since static members are declared in the class and operated outside of the class, it is somewhat special to perform the address fetch operation on them.
The variable address is a pointer to its data type, and the function address type is a "nonmember function pointer ".

(4) because the static member function does not have the this pointer, it is almost equivalent to the nonmember function.
This creates an unexpected benefit: Being a callback function allows us to combine C ++ and C-based X W
The indow system is combined and successfully applied to the thread functions.
(5) static does not increase the space-time overhead of the program. On the contrary, it shortens the access of the subclass to the static members of the parent class.
Time, saving sub-class memory space.
(6) Add the keyword "static" before <definition or description> to the static data member.
(7) static data members are stored statically, so they must be initialized.
(8) static member Initialization is different from general data member initialization:
Initialization is performed in the external class without static before, so as to avoid confusion with general static variables or objects;
During initialization, the access permission control characters private and public of the member are not added;
During initialization, the scope operator is used to indicate the class to which it belongs;
So we can get the format of static data member initialization:
<Data type> <class name >:< static data member name >=< value>
(9) to prevent the influence of the parent class, you can define a static variable of the same as the parent class in the subclass to avoid the influence of the parent class. Here, we need to note that static members are shared by the parent class and subclass, but we have repeatedly defined static members. Will this cause errors? No, our compiler uses a wonderful method: name-mangling is used to generate a unique flag.

Static Data Member

In a class, static members can share data between multiple objects. Using static data members does not undermine the hidden principle, which ensures security. Therefore, static members are shared among all objects of the class, rather than members of an object.

Static data members can save memory because they are public to all objects. Therefore, for multiple objects, static data members can only store one object for sharing. The value of a static data member is the same for each object, but its value can be updated. You only need to update the value of the static data member once to ensure that all objects have the same value after the update, which improves the time efficiency.

The usage and precautions of static data members are as follows:

1. When defining or describing static data members, add the keyword static.

2. static member Initialization is different from general data member initialization. The format of static data member Initialization is as follows:

<Data type> <class name >:< static data member name >=< value>

This indicates:

(1) initialization is performed in the external class without static before, so as to avoid confusion with general static variables or objects.

(2) during initialization, the access control letter private and public of the member are not added.

(3) during initialization, the scope operator is used to indicate the class to which it belongs. Therefore, static data members are members of the class, not members of the object.

3. static data members are stored in static mode, which indicates the lifetime of static data and must be initialized.

4. When referencing static data members, use the following format:

<Class name >:: <static member name>

If the access permission of the static data member is allowed (that is, the public Member), you can reference the static data member in the program in the preceding format.

Static member functions

Static member functions, like static data members, all belong to static members of the class and are not object members. Therefore, you do not need to use the object name to reference static members.

In the implementation of static member functions, non-static members described in the class cannot be directly referenced. Static members described in the class can be referenced. If a non-static member is to be referenced in a static member function, it can be referenced through an object.


The following is an example:
# Include <iostream. h>
Class Point
{
Public:
Void output ()
{
}
Static void init ()
{
}
};
Void main (void)
{
Point pt;
Pt. init ();
Pt. output ();
}
In this way, there will be no errors in compilation.
See below
# Include <iostream. h>
Class Point
{
Public:
Void output ()
{
}
Static void init ()
{
}
};
Void main (void)
{
Point: output ();
}
In this case, an error occurs during compilation. The error message is illegal call of non-static member function. Why?
Because the class is not allocated memory space when a specific object of a class is not instantiated.
Let's take a look at the following example:
# Include <iostream. h>
Class Point
{
Public:
Void output ()
{
}
Static void init ()
{
}
};
Void main (void)
{
Point: init ();
}
At this time, there will be no errors during compilation, because in the definition of the class, its static data and member functions have its memory zone, and it does not belong to any specific object of the class.
Let's take a look at the following example:
# Include <iostream. h>
Class Point
{
Public:
Void output ()
{
}
Static void init ()
{
X = 0;
Y = 0;
}
Private:
Int x;
Int y;
};
Void main (void)
{
Point: init ();
}
Compilation error:
Illegal reference to data member 'point: X' in a static member function
Illegal reference to data member 'point: y' in a static member function
A data member is incorrectly referenced in a static member function,
Still, static members (functions) do not belong to any specific object. Therefore, the memory zone is available before the specific object Declaration of the class,
But now non-static data members have not allocated memory space, so the call here is wrong, as if they were not declared but used in advance.
That is to say, non-static member variables cannot be referenced in static member functions.
Let's take a look at the following example:
# Include <iostream. h>
Class Point
{
Public:
Void output ()
{
X = 0;
Y = 0;
Init ();
}
Static void init ()
{

}
Private:
Int x;
Int y;
};
Void main (void)
{
Point: init ();
}
Okay, so there will be no errors. This is eventually a memory model problem,
If any variable has its own space in the memory, it can be called elsewhere. Otherwise, an error will occur.
Let's take a look at the following example:
# Include <iostream. h>
Class Point
{
Public:
Void output ()
{
}
Static void init ()
{
X = 0;
Y = 0;
}
Private:
Static int x;
Static int y;
};
Void main (void)
{
Point: init ();
}
Compile:
Linking...
Test. obj: error LNK2001: unresolved external symbol "private: static int Point: y"
Test. obj: error LNK2001: unresolved external symbol "private: static int Point: x"
Debug/Test.exe: fatal error LNK1120: 2 unresolved externals
An error occurred while executing link.exe.
We can see that there are no compilation errors and connection errors. Why?
This is because static member variables need to be initialized, which can be as follows:
# Include <iostream. h>
Class Point
{
Public:
Void output ()
{
}
Static void init ()
{
X = 0;
Y = 0;
}
Private:
Static int x;
Static int y;
};
Int Point: x = 0;
Int Point: y = 0;
Void main (void)
{
Point: init ();
}
No compilation error will occur after static member data variable initialization.
Let's look at the following code:
# Include <iostream. h>
Class Point
{
Public:
Void output ()
{
}
Static void init ()
{
X = 0;
Y = 0;
}
Private:
Static int x;
Static int y;
};
Void main (void)
{
}
No compilation error. Why?
Even if they are not initialized, the compilation will not go wrong because we have not accessed x, y.


C ++ differentiates two types of member functions: static member functions and non-static member functions. One major difference between the two is that static member functions do not accept the implicit this independent variable. Therefore, it cannot access non-static members of its own class.

Under some conditions, for example, when using a multi-threaded Library such as pthread (which does not support classes), a static member function must be used because its address is compatible with the address of the C language function. This copper limit forces programmers to use various solutions to access non-static data members from static member functions.

The first solution is to declare that all data members of the class are static. In this way, static member functions can directly access them, for example:

Class Singleton
{
Public:
Static Singleton * instance ();
Private:
Singleton * p;
Static Lock lock;
};

Singleton * Singleton: instance ()
{
Lock. getlock (); // fine, lock is static
If (! P)
P = new Singleton;
Lock. unlock ();
Return p;
}

This solution is not applicable to classes that require non-static data members.

Access non-static data members

Pass the reference to the object to be considered so that static member functions can access non-static data of the object:

Class
{
Public:
Static void func (A & obj );
Intgetval () const; // non-static member function
Private:
Intval;
};

The static member function func () uses the reference obj to access non-static member val.

VoidA: func (A & obj)
{
Int n = obj. getval ();
}

Passing a reference or pointer as the independent variable of the static member function means that the row of this independent variable in the non-static member function is imitated.

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.