How to use static in C++

1. Static data members

    Before introducing static data members, let's take a look at ordinary data members:

class complex
{
public:
  double real() const {return this -> re;} //In the parameter list (parentheses) of member functions, there is a hidden parameter this pointer, which cannot be written.
private:
  double re,im;
}
complex c1,c2;
cout << c1.real();
cout << c2.real();

When calling a function through an object, the form is actually equivalent to:

complex c1,c2,c3;
cout << complex::real(&c1);
cout << complex::real(&c2);

Simply put, whoever calls the member function is equivalent to this pointer. In the above program, the address of c1 becomes this pointer.

    Static data member: The static keyword is added before the data member declaration in the class, and the data member becomes the static data member of the class. Static data members have the following characteristics:

(1) Static data members are shared by all objects, including the objects of the derived class of this class, that is, the derived class objects and the base class objects share the static data members of the base class. The static members of a class belong to the class and are directly related to the class itself, not to which object, so it can be said that it is shared by all objects;

(2) Like ordinary members, static data members can be public and private;

(3) Static data members should be declared in the class body, and defined and initialized outside the class body;

(4) When referencing static data members, add <class name> and scope operator before the static data members.

2. Static member functions

    Static member function: a member function that is decorated with static in a class. Has the following features:

(1) The address of the static member function can be stored with the ordinary function pointer, and the address of the ordinary member function is stored with the pointer of the class member function;

(2) Static member functions can only handle static data, because static member functions do not have this pointer;

(3) Static member functions cannot be declared as virtual or const functions.

Let's look at an example of static data members and static member functions:

class Account
{
public:
  static double m_rate;//Static data members, declared in the class
  static void set_rate(const double& x) {m_rate = x;}
};
double Account::m_rate = 8.0;//Static data members are initialized outside the class

intmain()
{
  Account::set_rate(5.0);//Call static function by class name

  Account a;
  a.set_rate(7.0);//Call the static function through the object (because there is no this pointer in the static member function, it can be called like this)
}

       The static function does not contain the hidden this pointer provided by the compiler. It exists when the class is not instantiated, so it can be used directly, and since there is no this pointer, there is no specific member variable for it to use, because Before the class is instantiated, these class member variables do not exist, the system does not allocate space to these variables, and there is no this pointer, and these member variables cannot be called, so he can only use those that already exist before the class is instantiated. static variable. At the same time, static members can also be accessed independently, that is, they can be accessed without creating any object instance.

       Ordinary member functions pass a default this pointer when they are created, which means that each object has its own set of member variables, which means that it can use these member variables, and it can also use static member variables, because these variables It exists .

       Ordinary member functions are called through objects, so an object must be created first; while static member functions can be used without creating an object. Therefore, member functions that have nothing to do with non-static data members of a class can be defined as non-static functions, but it is more convenient to use them if they are defined as static functions .


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325891595&siteId=291194637