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

后端 未结 9 1005
别那么骄傲
别那么骄傲 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:28
    global.h
    extern int myVar;
    
    global.cpp
    #include "global.h"
    int myVar = 0;  // initialize
    
    class1.cpp
    #include "global.h"
    ...
    
    class2.cpp
    #include "global.h"
    ...
    
    class3.cpp
    #include "global.h"
    ...
    

    MyVar will be known and usable in every module as a global variable. You do not have to have global.cpp. You could initialize myVar in any of the class .cpp's but I think this is cleaner for larger programs.

    0 讨论(0)
  • 2020-12-13 15:31

    The below solution should be simple enough as per the title "How to declare a global variable that could be used in the entire program " . If you want to use it in a different file then make use of extern keyword.

    Please let me know if there is any issue with the solution

    #include<iostream>
    
    using namespace std;
    
    int global = 5;
    
    class base {
        public:
            int a;
            float b;
            base (int x) {
                a = global;
                cout << "base class value =" << a << endl;
            }
    };
    
    class derived : public base {
        public:
            int c;
            float d;
            derived (float x, int y) : base (y)
            {
                d = global;
                cout << "derived class value =" << d << endl;
            }
    };
    
    
    int main ()
    {
        derived d(0,0);
        cout << "main finished" << endl;
        return 0;
    }
    
    0 讨论(0)
  • 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;
    
    0 讨论(0)
  • 2020-12-13 15:38

    While I would like to avoid global variables like the plague as our software cannot be multithreaded effectively due to the high reliance on global variables, I do have some suggestions:

    Use a Singleton. It will allow you to keep the code and access clean. Part of the problem with a global variable is you don't know what code has modified it. You could set the value of global somewhere in your function relying on the hope that no one else will change it, but function your code calls, fooA, changes it and now your code is a) broken, and b) hard to debug.

    If you have to use a global variable without touching the singleton pattern, look at fupsduck's response.

    0 讨论(0)
  • 2020-12-13 15:42

    Just declare it outside the class:

    Header file:

    extern int x;
    
    class A {
      int z;
      public:
      A() : z(x++) {}
    };
    

    One source file:

    int x = 0;
    
    0 讨论(0)
  • 2020-12-13 15:43

    If you're not going to use the Singleton pattern as Lyndsey suggests, then at least use a global function (inside a namespace) to access the variable. This will give you more flexibily in how you manage that global entity.

    // mymodule.h
    namespace mynamespace // prevents polluting the global namespace
    {
       extern int getGlobalVariable();
    }
    
    // mymodule.cpp
    namespace mynamespace
    {
       int myGlobalVariable = 42;
    
       int getGlobalVariable()
       {
          return myGlobalVariable;
       }
    }
    
    0 讨论(0)
提交回复
热议问题