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

后端 未结 9 1039
别那么骄傲
别那么骄傲 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:37

    if you want to declare it in different header files/cpp files, just declare it extern outside of other files

    //file1.c
    int x=1;
    int f(){}
    //file2.c
    extern int x;
    

提交回复
热议问题