Why are C++ static variables defined outside the class?

1. Background

Encountered the problem of undefined variables, as follows:

 1 #include <iostream>
  2 #include <string.h>
  3 using namespace std;
  4 
  5 
  6 class sample
  7 {
    
    
  8 private:
  9 static int s_s1;
 10 int m_s2;
 11 
 12 public:
 13 sample(){
    
     m_s2 = 0; cout << "default constructor! "<< endl;}
 14 sample(sample & s){
    
     cout <<"copy constructor! " << endl;}
 15 void show(void);
 16 };
 17 
 18 void sample::show(void)
 19 {
    
    
 20     s_s1++;
 21     m_s2++;
 22     cout << s_s1 << endl;
 23     cout << m_s2 << endl;
 24 }   
 25 
 26 int main(void)
 27 {
    
    
 28 sample e1;
 29 e1.show();
 30 sample e2;
 31 e2.show();
 32 
 33 }
 34 
~                                                                                                                                                                                              
~      
root@mkx:~/learn/staticInClass# g++ staticInClass.cpp -o staticInClass
/tmp/ccK5xzjv.o:在函数‘sample::show()’中:
staticInClass.cpp:(.text+0xe):对‘sample::s_s1’未定义的引用
staticInClass.cpp:(.text+0x17):对‘sample::s_s1’未定义的引用
staticInClass.cpp:(.text+0x2c):对‘sample::s_s1’未定义的引用
collect2: error: ld returned 1 exit status
root@mkx:~/learn/staticInClass#                                  

2. Examples

Changed to the following, the problem is solved:

 1 #include <iostream>
  2 #include <string.h>
  3 using namespace std;
  4 
  5 
  6 class sample
  7 {
    
    
  8 private:
  9 static int s_s1;
 10 int m_s2;
 11 
 12 public:
 13 sample(){
    
     m_s2 = 0; cout << "default constructor! "<< endl;}
 14 sample(sample & s){
    
     cout <<"copy constructor! " << endl;}
 15 void show(void);
 16 };
 17 int sample::s_s1 = 0;
 18 
 19 void sample::show(void)
 20 {
    
       
 21     s_s1++;
 22     m_s2++; 
 23     cout << s_s1 << endl;
 24     cout << m_s2 << endl;
 25 }
 26 
 27 int main(void)
 28 {
    
    
 29 sample e1;
 30 e1.show();
 31 sample e2;
 32 e2.show();
 33 
 34 }
 35 
~                                                                                                                                                                                              
"staticInClass.cpp" 35L, 455C 已写入                   

root@mkx:~/learn/staticInClass# g++ staticInClass.cpp -o staticInClass
root@mkx:~/learn/staticInClass# ./staticInClass 
default constructor! 
1
1
default constructor! 
2
1
root@mkx:~/learn/staticInClass# 

3. Summary

Why C++ static member variables must define
functions outside the class is as follows. When declaring a static member variable in C++, it is only declared in the class and does not actually apply for the memory of the pointer. The real memory is defined and initialized. Only then will the application for memory be made.
Why is this so?
Because the variables of static type are all associated with the class, memory cannot be applied for along with the creation of the object, so it needs to be defined separately outside the class. When defining, the C++ compiler will apply for memory for the static pointer.
As shown in the figure:
image.png
it does not belong to the object, so it cannot be created along with the object, so it can only be defined outside the class.

Guess you like

Origin blog.csdn.net/maokexu123/article/details/126530356