| re: saving the value from one file to other
I'm not sure what you mean:
1) access the vector from another file??
2) access a value retreived from the vector??
The ideal answer is:
1) put the vector in a file.
2) put a function in that file that can access the vector.
3) call that function from the other files.
You do not want to extern the vector. If you do, and you later need to chnage the vector to something else, you will have to change all of your code. If you use a function to access the vector, you just need to change that function to implement a new data structure.
Extern implies a global variable and global variables need to be avoided because:
1) local variable hides global variable
2) name conflicts. even within namespaces.
3) exposes implementation. No redesign
4) causes race conditions in multithreaded programs
5) expands ripple when value is screwed up. every function is a suspect
6) no guarantee the user will use global
7) expands program footprint
8) memory for globals may be limited
9) no guarantee for the order of creation. Only the globals in a single file are crerated in the order of declaration. The total order is indeterminate. Hence, your global may no be there when you need it. Especially if a global object needs a global variable in another file in its constructor. aka: the initialization fiasco
10) expands ripple when a recode is needed. All the code using globals has to be changed. Bad if there is a large installed base.
Partial solution:
1) use a namespace or function interface to an anonymous namespace
Best solution:
Use a singleton. destructors can be private to prevent the singleton for being deleted. Read the Singleton article in the C/C++ Articles section.
|