The difference between regular and static data members in a C++ class

  • 2020-04-02 01:50:53
  • OfStack

Just began to learn C++ class and object part, the constant data in the class and static data members of the concept and usage is often confused, so today tidy up, by the way, today is my birthday, wish me a happy birthday, ha ha.

Constant data member
Constant data members are data members defined in a class whose values cannot be modified. They are similar to the constant variables we have learned before. Although they are variables, they have their own address, but once they are assigned an initial value, they cannot be modified.

Applies to defining variables in a class that are not intended to be modified after initialization.

Definition method:


const  Type name   The variable name ;

(1) in class Constant data members can only be initialized by the constructor's parameter initialization table.

(2) the constant data member is a part of the instantiated object and can be accessed with this pointer.


#include<iostream>
using namespace std;
class Happy_birthday
{
 public:
   Happy_birthday(char * na):name(na){}
   void Print()
   {
    cout<<name<<" Happy Birthday!"<<endl;
   }
 private:
  const char * name;
};
int main()
{
 Happy_birthday qianshou(" Zhao Zhe ");
 qianshou.Print();
 return 0;
}

Operation results:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/20130920004315218.png ">

Static data member
In fact, regular data members and static data members are completely different things, but it is easy to confuse them at first. From the above explanation, we know that the constant data member is similar to the constant variable, which cannot be changed once the value is assigned.

The big difference is that the static data members can be modified, and can be modified by any object, and the modified values can be Shared by all objects.

A static data member is a member of a class, not an object, and is Shared by all objects defined for that class. All objects defined by this class can reference this static member, and the values are the same.

The storage space of static data members is different from that of ordinary data members. It does not belong to any object of the class and is stored independently of the object This pointer to the object cannot be accessed.

Moreover, static data members cannot initialize tables with parameters, for the simple reason that initializing tables is done when defining objects using this pointer, so it is not possible.

The way static data members are defined:


static int num;

Access method:

Can be accessed directly through the class name:


Test::s_num;

It can also be accessed by object name:

one.s_num ; 

However, in order to distinguish it from other member variables, it is common to use the class name for access, because the static data members do not belong to the object, so as not to be misunderstood.

#include<iostream>
using namespace std;
class Test
{
 public:
  Test(int n):c_num(n){};//An initial value can only be assigned to a constant data member using an initialization table
  void show()
  {
   cout<<"c_num:"<<this->c_num<<endl;
   cout<<"s_num:"<<s_num<<endl;
  }
  void change(int n)
  {
   s_num=n; 
  } 
  static int s_num;
 private:
  const  int c_num;
};
int Test::s_num=100;//Assigns an initial value to a static data member in the class body
int main()
{
 Test one(10);
 one.show();
 one.change(10000);//Changes the value of the static data member
 cout<<"one changeed:"<<one.s_num<<endl; //Use the object name one to indirectly access static data members
 cout<<"Test changeed:"<<Test::s_num<<endl;//Use the class name to access static data members directly
 Test two(20);
 cout<<"two changeed:"<<two.s_num<<endl; //Use the object name two to access the static data member indirectly
 two.change(99); 
 cout<<"Test changeed:"<<Test::s_num<<endl;//Use the class name to access static data members directly 
 return 0;
}

Operation results:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/20130920011411218.png ">

Analysis: We can use the class name in the program to directly access the static data member variable, which directly illustrates that it does not belong to any object. We only assigned c_num when we defined object two, but we can output it when we use two-s_num, which also indicates that this data member can indeed be referenced by all objects, and the value is the same, because it does not belong to any object.


Related articles: