C++ class static member and class static member function

  • 2020-04-02 01:43:39
  • OfStack

When a data member of a class is declared static, the static data member can only be defined once and is Shared by all objects of the same class. Each object has a copy of every normal data member in the class, but only one instance of the static data member exists, regardless of how many class objects are defined. A static method is simply associated with the class, a behavior of the class, not an instance object of the class.

One of the purposes of static data members is to count how many objects actually exist.

Static data members cannot be initialized in a class, and in fact the class definition only describes the blueprint of the object, in which initial values are not allowed. You also cannot initialize the member in the constructor of the class, because the static data members are Shared among the objects of the class, or the static data members are reinitialized each time an object of the class is created.

A static member cannot be assigned inside a class because it is Shared by all objects of that class. If you assign a value to an object, the member in the other object changes. To avoid confusion, you cannot assign values inside a class.

The value of the static member is the same for all objects. Static members can be initialized, but only outside the class.

General form:
Data type class name: : static data member name = initial value
Note: A static member cannot be initialized with a parameter initialization table. General systems start at 0 by default.

A static member is a Shared member of all objects in a class, not a member of an object. It takes up no storage space in the object, and this property is Shared by the entire class and does not belong to any specific object. Therefore, static members cannot be initialized inside the class. For example, if a student class is declared and one of the members is the total number of students, then this variable should be declared as a static variable, and the member variables should be set according to the actual requirements.


#include "iostream"
using namespace std;
class test
{
private:
     int x;
     int y;
public:
     static int num;
     static int Getnum()
     {
          x+=5;   //This line of code is incorrect. A static member function cannot call a non-static data member, but must be called through an object of the class.
          num+=15;
          return num;
     }
};
int test::num = 10;
int main(void)
{
     test a;
     cout<<test::num<<endl;        //10
     test::num = 20;
     cout<<test::num<<endl;        //20
     cout<<test::Getnum()<<endl;   //35
     cout<<a.Getnum()<<endl;       //50
     system("pause");
     return 0;
}

As can be seen from the above example:   X + = 5;     // this line of code is wrong
Static function members must access non-static data members by object names.
In addition, the static member function is implemented outside the class without static keyword, otherwise it is wrong.
If the above static member function is implemented outside of the class, the static keyword cannot be added, so it can be written:

     int test::Getnum()
     {
       .........
     }

1. The static member is owned by the class itself and the object, but most objects have the same static member. Thus in the definition of the object is not through the constructor to initialize it.

2. A static member cannot be initialized inside the class definition, but only outside the class body.

3. Static members still follow the guidelines of public, private and protected access.

A static member function does not have this pointer, and it cannot return a non-static member, because the class itself can call it as well as the object.

Static member functions can access static data and function members of the class directly, while non-static data members must get an object name by passing an argument and then access it by object name.


class Myclass
{
private:
        int a,b,c;
        static int Sum;    //Declare static data members
public:
        Myclass(int a,int b,int c);
        void GetSum();
};
int Myclass::Sum=0;       //Define and initialize static data members
Myclass::Myclass(int a,int b,int c)
{
        this->a=a;
        this->b=b;
        this->c=c;
        Sum+=a+b+c;
}
void Myclass::GetSum()
{
        cout <<"Sum=" <<Sum <<endl;
}
int main(void)
{
        Myclass me(10,20,30);
        me.GetSum();
        system("pause");
        return 0;
}

From the above example, non-static member functions can arbitrarily access static member functions and static data members.
The non-static member functions Myclass(int a,int b,int c) and GetSum() both access the static data member Sum.
Static member functions cannot access non-static member functions and non-static data members.

The static member function can be summarized as the following:

Function definitions that appear outside a class cannot specify the keyword static;

Static members can access each other, including static member function access static data members and static member function access;

Non-static member function can access static member function and static data member arbitrarily;

Static member functions cannot access non-static member functions and non-static data members;

Since there is no overhead associated with this pointer, the static member function is slightly faster than the global function of the class.

Call the static member function, and you can use the member access operators (.) and (-) > Call the static member function for the object of a class or pointer to the class object. When all objects of the same class use a quantity, for the common quantity, you can use the static data member variable, which takes the same value for all objects of the same class. Static member variables can only be called by static member functions. Static member functions are also Shared by all objects in the same class. You can only call static member variables and static member functions.


Related articles: