How to declare a global variable that could be used in the entire program

后端 未结 9 1025
别那么骄傲
别那么骄傲 2020-12-13 14:52

I have a variable that I would like to use in all my classes without needing to pass it to the class constructor every time I would like to use it. How would I accomplish th

相关标签:
9条回答
  • 2020-12-13 15:44
    // L.hpp
    struct L { static int a; };
    
    // L.cpp
    int L::a(0);
    
    0 讨论(0)
  • 2020-12-13 15:45

    keyword extern

    //file1.cpp
    
    int x = 0;
    
    //file1 continues and ends.
    
    
    //file2.cpp
    
    extern int x; //this gets tied into file1.cpp's x at link time.
    
    //file2.cpp goes on and ends
    
    0 讨论(0)
  • 2020-12-13 15:45

    Declare the variable as extern in a common header.

    Define it in any source file.

    0 讨论(0)
提交回复
热议问题