How can I use variables in multiple files?

后端 未结 3 1962
旧时难觅i
旧时难觅i 2021-01-16 04:51

I want do define a variable in the header and be able to use it in multiple files. e.g. have variable a defined somewhere and be able to use/change it in both

3条回答
  •  孤街浪徒
    2021-01-16 05:48

    Collin already described the problem and the solution, but I want to emphasize just how bad a style this is.

    As you've found out even from this simple exercise, using globals in this manner is often a Bad Thing. Aside from the compiler issues you've already found, sharing state between functions using globals causes the following problems:

    • it makes the function itself harder to reuse in other programs, because the function is expecting the presence of those symbols;

    • changes to one function may cause problems in other functions that share that state (i.e., the changes are not localized to the function);

    • code becomes harder to debug, since anyone can modify a and b at any time.

    Ideally, functions should communicate exclusively through parameters, return values, and exceptions (where applicable); there are times where sharing state is the right answer, but those are relatively few and far between.

    Since you're obviously using a C++ compiler, I'm going to do this the C++ way, using references instead of pointers. I'm also going to use C++ iostream routines instead of cstdio (write either C or C++; trying to mix the two only leads to heartburn).

    vars.hpp:

    #ifndef VARS_H  // Include guard to prevent multiple inclusion
    #define VARS_H
    
    void double_vars(int &, float &);
    
    #endif
    

    vars.cpp:

    #include "vars.hpp"
    
    void double_vars(int &x, float &y)
    {
      x *= 2;    
      y *= 2.0;  
    }
    

    main.cpp:

    #include 
    #include "vars.hpp"
    
    int main(void)
    {
      int a = 1;
      float b = 2.2;
    
      std::cout << "a: " << a << "; b: " << b << std::endl;
      double_vars(a, b);
      std::cout << "a: " << a << "; b: " << b << std::endl;
    
      return 0;
    }
    

    Since x and y were declared as reference types (using the &), writing to the expressions x and y in double_vars is equivalent to writing to a and b in main.

    The C way of doing this would be with pointers instead of references:

    vars.h:

    #ifndef VARS_H  // Include guard to prevent multiple inclusion
    #define VARS_H
    
    void double_vars(int *, float *); 
    #endif
    

    vars.c:

    #include "vars.h"
    
    void double_vars(int *x, float *y)
    {
      *x *= 2;    
      *y *= 2.0;  
    }
    

    main.c:

    #include 
    #include "vars.h"
    
    int main(void)
    {
      int a = 1;
      float b = 2.2;
    
      printf("a: %d; b: %f\n", a, b);
      double_vars(&a, &b);
      printf("a: %d; b: %f\n", a, b);
      return 0;
    }
    

    In this case, we're using pointers instead of references, so we need to pass the result of the expressions &a and &b to double_vars; within the function, writing to the expressions *x and *y is the same as writing to a and b in main.

提交回复
热议问题