开发者

Manipulating global variables

开发者 https://www.devze.com 2023-03-06 02:04 出处:网络
I have functions in two (linked) C++ files that both use the same variable. To me (a complete beginner) I could see two ways of handling this:

I have functions in two (linked) C++ files that both use the same variable. To me (a complete beginner) I could see two ways of handling this:

  1. Pass the variable from the first function to the second (they are sequential in the program), or

  2. Define the variable as a global value in a separate header file that both c++ files includ开发者_运维技巧e at the beginning

Whilst (1) may seem like the most obvious answer, I chose to use 2. For whatever reason.

Anyway, the variable is now declared in the header, but if I try to assign a value to the variable at the time of declaration, I get the error "(value) already defined in main.obj".

The header is included at the beginning of both C++ files. The variable is not declared anywhere else, but it is used.

There must be an obvious answer to this, but I'm very new to C++. Anyone who can shed light on why I can't assign the variable a value I would be very grateful to.


Use extern: http://bobobobo.wordpress.com/2009/06/04/understanding-extern-variables-in-c/


Since you declare the variable in header and then include the header in two separate files you end up defining the same variable in two places.
A possible way or organizing them can be:

myList.h

class list { ....};  

file1.cpp

#include "myList.h"
list * x ;

file2.cpp

#include "myList.h"
extern list * x;  


While you can resolve the issue of how to use a global, it is much better if you avoid it altogether. If you have two functions that modify a single variable, then the simplest thing you can do is to pass the variable as argument to the function:

void foo( type & var );
void bar( type & var );
int main() {
   type t( constructor_argument );   // [1]
   // [2] operate on t before calling the functions
   foo( t );
   // [3] operate some more if needed
   bar( t );
   // [4] t now has been updated by both foo and bar
}

If the variable does not need to be constructed before the first function ([1] not required, and [2] not present), then you can have that variable as a return value of the function:

type foo(); // create and return the variable
void bar( type& );
int main() {
   type t = foo();
   // [3] operate on t
   bar( t );
   // [4] use the final t
}

If you don't need to update the variable after calling the first function, and the second function does not actually modify the variable (it only reads what the first function did) and the variable is not needed in main after the second function execution ([4] is empty), then you can change the second function signature and avoid the variable in main altogether:

type foo();
void bar( type );
int main() {
   bar( foo() );
}

Any and all of the solutions to the different problems shown here are much better than having a global variable that is used by different functions. When you read the code in main (or whatever the function that has the logic) it is obvious how the data is flowing from one point to another. In the case of globals, the need of calling foo before calling bar is not explicit in the code, the dependency on the global is hidden unless you read the implementations of the functions.

Avoid globals as much as possible, it will make your code more maintainable, easier to reason about and easier to test.


Create a class with these two functions (you could add some more) and member variable in it. Using global variables are usually considered as bad practice in C++.

0

精彩评论

暂无评论...
验证码 换一张
取 消