|
|||||||
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
hello,
I can't figure out how to create an array that can be called from any module. Global or public sounds familiar but they don't work. Cheers. |
|
#2
|
|||
|
|||
|
int numz[10] = {0};
..... void fill_in(int* p, int size) { for(int i = 0; i < size; i++) p[i] = i; } ....... fill_in(numz, 10); ------------------------------------------- If you are trying to make a global instead, you need to put the extern thing in your .h files as with C code. However, do not do this unless you have an extremely good reason, globals are messy. You should probably study how to do this and recognize it though, if for no other reason when you see it you immediately know the code is going to get ugly. in c++, at least wrap your globals in a namespace, if not in a struct, just to qualify them for the reader. Better, put them in a class as static variables, locally create an instance of the class to read and write them: class global_stuffs { public: static int x[50]; ...... ///more stuff global_stuffs() {//initilize your globals here, BE WARNED PROTECT THE CTOR ONLY DO IT ONCE} }; int global_stuffs::x[50]; void foo() { global_stuffs access; access.x[0] = 10; //this is global, even though access is local you are touching a global, static object so each instace of the class uses the same memory and same global variable. } void bar() { global_stuffs same; printf("%i\n", same.x[0]); //will print what was set in foo! } Remember this stuff is not thread safe nor particularly wise to do, a global is still a global and the problems they cause are all still there, its just wrapped up to look nicer and be a touch more clean. You can *add* things to your global class to protect for threading and to handle other problems with globals but thats your problem -- it balloons the code up fast and is mostly bloat if you do not need it. Note: you can actually fool with the globals directly via global_stuffs::x[0]; because the memory persists without an object of the class. In fact, you want to be careful with any code in your constructor for that reason: making a new instance will delete the persistant value if the constructor modifies them all back to a default! You can add the do once code to the ctor of course, but as I said, it quickly becomes hackery no matter how you want to address the problem. (Also, I am in the minority here, but the entire object oriented paradigm is actully a clever way to make everything more or less global in a nice, clean way in my opinion. That being the case, you really shouldnt need any of my stuff here at all: a good design should remove the need for globals period, and the only reason to have them is to work around a design flaw or for speed (they are faster, and sometimes it matters, but this is not the norm). Last edited by jonnin; 10-23-2009 at 10:44 AM. |
|
#3
|
||||
|
||||
|
You need to declare the array as a global variable. Define it in one module in the global namespace, say before main()
Then declare that same array as extern in any other module that refers to the array. //module 1 int arr[5]={0,1,2,3,4}; int main() {} //module 2 extern int arr[5]; arr[0]=9;
__________________
Danny Kalev |
|
#4
|
|||
|
|||
|
Thanks guys that was really helpful! But is there anyway to apply this to a class? So that info about a wav file could be read from multiple modules?
Thanks again! |
|
#5
|
|||
|
|||
|
Quote:
Do you mean, how can class A share its data with class B? This is not done by globals, its done by object oriented techniques such as polymorhpism (assign A into B and the correct fields are copied over) or inheritance (B uses the data from A directly, or the getter function of A, etc) or the like. Remember when I said the point of objects is really a clever disguise to make everything global (ok, its really a clever way to control who can get at what, but it certainly can make everything open to everything else in a global sense if that is what you like, or lock it out if you prefer, etc.). Or you can handle it with simple parameter passing, or have another object C that both A and B use, or dozens of other ways. Even a simple assignment operator overload can be used (B = A). In your program, can B see A? If so, a simple solution is very possible. If not, a more complex solution is needed. Do you want to share the data or copy it? Can they ever be different (A != B)? These sorts of questions help you pick a solution that is right for your design. Again, try to avoid the true global approach unless you are 100% sure its the only way out. Even then, better to fix the underlying issue if you have time. |
|
#6
|
|||
|
|||
|
I mean modules as in adding another source of code to the project. from main i'd #include "new module.h"
I can define a variable in the main code file, using a class that stores information about a WAV file, but if i try and use that variable within a function of the module, it tells me that the variable isn't defined. What I need is a way to define the variable, then be able to access its data in any function, in any code source. Cheers, Mike |
|
#7
|
||||
|
||||
|
As I said, define the array in the global namespace and then declare it 'extern' in every translation unit (that's the correct term, btw) that uses it.
__________________
Danny Kalev |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| modules as DLL's? | vchatlive | VB Classic | 1 | 07-13-2006 04:48 PM |
| Access Modules | Hemant Sathe | VB Classic | 2 | 07-23-2001 11:11 AM |