Accessing a global object is sometimes necessary for all objects of a particular class type. At any point in the program's operation, you may need to count the number of objects of a particular class type that have been created, but the global object will break the encapsulation and there is no security. Normal user code can modify this value.
Classes can define static members to solve the problem of data and function sharing between different objects of the same class, using the same memory space for static members of different objects of a class. Static members contain static data members and static function members.
Usually, non-static data members exist in every object of the class type, but static data members exist independently of any objects of the class. Each static data member is an object associated with the class and is not associated with an object of the class. .
Static member functions do not have a this parameter, you can directly access the static member of the class, but you can not directly use non-static members.
Advantages of using static members:
(1) Conducive to class encapsulation, static members can be defined as private members to prevent external access;
(2) The static member is associated with a specific class, and the class name must be prefixed externally, and the program is clearer;
(3) The name of the static member is in the scope of the class, which can avoid naming conflicts.
(1) Define static members
Static follows normal public and private access restrictions. Every member that needs to be defined as static needs to be static before it, which is different from the access limit.
Static member functions can be defined directly after the declaration or outside the class. When defined outside the class, the static keyword is not needed.
In static member functions, the this pointer cannot be used because static member functions are common to all objects of the same class. Non-static data members can also be used in the same static member function, and only static data members of the class can be used.
Class Student
{
Public:
Static int getCount();
Static int count;
};
Unlike ordinary data members, static data members must be defined and initialized outside of the class definition body. The definition must be prefixed with the class name.
Int Student::count=0;
(two) using static members
You can use the scope operator "::" to call a static member from a class, or indirectly through an object, a pointer to an object of that class type. If you can use the following methods to access static members.
Student::count=0;
Int n=Student::getCount();
Static members can also be accessed through class objects:
Student stu1;
Stu1.count=0;
Student *s=&syu1;
Int n=s->getCount();
When static members are used inside a class, they can be used directly, without the scope operator. You can also use static members with methods that are not static members.
2.1 Using static data members
#include
Using namespace std;
Class Test
{
Public:
Static int n;
Test(int x)
{
k = x;
n++;
}
Void disp()
{
Cout << "n= " << n << ", k=" << k << endl;
}
Private:
Int k;
};
Int Test::n = 0;
Int main()
{
Test t1(10);
T1.disp();
Test t2(20);
T2.disp();
Test::n++;
T2.disp();
System("pause");
Return 0;
}
2.2 Using static member functions
Static member functions can directly reference static data members and member functions of the class, but not non-static members. If you want to reference a non-static member, you must get the object name by parameter passing, and then refer to it by the object name. Issues to be aware of when using static member functions:
Static member functions can be defined inside the class, or outside the class. You don't need to add the keyword static when defining the class.
The system defines the static member function as an internal connection, so that the security of the static member function is guaranteed because it conflicts with other member functions of the same name in the connection file.
There is no implicit this pointer in the static member function;
#include
Using namespace std;
Class Dot
{
Static int t;
Int a, b;
Public:
Dot(int x = 0, int y = 0)
{
a = x;
b = y;
t++;
}
Dot(Dot &d);
Int geta()
{
Return a;
}
Int getb()
{
Return b;
}
Static void gett()
{
Cout << "object id:" << t << endl;
}
};
Dot::Dot(Dot &d)
{
a = da;
b = db;
t++;
}
Int Dot::t = 0;
Int main()
{
Dot::gett();
Dot d1(2, 3);
Cout << "Dot d1:" << d1.geta() << "," << d1.getb() << endl;
D1.gett();
Dot d2(d1);
Cout << "Dot d2:" << d2.geta() << ", " << d2.getb() << endl;
Dot::gett();
System("pause");
Return 0;
}
The Mylar Speaker solutions come in a variety of shapes and sizes. We can provide various mounting configurations and performance alterations to fit any industrial application. Our Mylar speakers produce excellent sound output (dB) at specified frequency ranges. The unique characteristics of a Mylar cone allow us to keep tight tolerances during the manufacturing process. Additionally, Mylar is easily and consistently moldable, creating a cost-effective solution, time after time. It`s also beneficial in applications that are exposed to excessive moisture or humidity since Mylar has a high resistance to environmental factors.
Mylar Speaker,Mylar Tweeter,Mylar Cone Speaker,Cellphone Mylar Speaker
Jiangsu Huawha Electronices Co.,Ltd , https://www.hnbuzzer.com