How can I define global arrays, vars, etc inside a function? I want to access these objects from other .cpp files.
Printable View
How can I define global arrays, vars, etc inside a function? I want to access these objects from other .cpp files.
Why are you trying to define them inside a function? If they are global than why not declare them at the top of the file?
Don't ever declare global variables. If a function needs a variable, then pass it to the function.Quote:
How can I define global arrays, vars, etc inside a function?
>>Why are you trying to define them inside a function? If they are global than why not declare them at the top of the file?
I want to put a function in a .lib that should define some objects. Other functions in that lib and even other .cpp files that had included the header of the lib should be able to use these objects.
>>Don't ever declare global variables. If a function needs a variable, then pass it to the function.
Objects should be defined in a function, but the caller of functions which use those objects is not that.
Forum displayed wrong last post.
The caller is also a function...Quote:
Objects should be defined in a function, but the caller of functions which use those objects is not that.
I mean the "caller function" is not the "definer function". You think I don't know that caller is function?
My question was simple guys. Defining Global inside a block.
Is there any way to do so, or not?
No. You cannot define a global inside a block. By definition, a global is a variable that's defined outside any block.
It was possible, however, at least in C, to declare globals inside a block, using the extern keyword. Not sure if that's still legal in C++, it might be one of the places where the language was changed.
I don't know C but in C++, extern tells compiler "This object will be defined somewhere else so don't allocate any space to them in memory". And it's not what I want.
Yes, it makes a declaration instead of a definition. As I said, it is not possible to have a global definition inside a block.
Not that there is any compelling reason for having this. Globals are global, and attempting to define them inside blocks therefore nonsense.
Note that while you can't define a variable with global scope in a function block, you can define a variable with global lifetime by using the static qualifier.
I know, but as I said all of my functions should have access to objects. I know the best solution for my problem is using a class, but I don't want to do so.
have the function return the objects. having that kind of random global access to local variables is a receipe for disaster and the very concept of it should deeply offend you.
I still don't get where you see a difference between what you want to do and normal global variables. What's so difficult about writing a header and a source file?
globals.h
globals.cppCode:#ifndef GLOBALS_H_LIGJJSAIEWJRKADLKE
#define GLOBALS_H_LIGJJSAIEWJRKADLKE
extern int global_number_one_with_big_ugly_name_to_discourage_you_from_using_it;
extern int global_number_two_with_big_ugly_name_to_discourage_you_from_using_it;
#endif
What would the semantic differences of whatever you have in mind be?Code:#include "globals.h"
int global_number_one_with_big_ugly_name_to_discourage_you_from_using_it = 1;
int global_number_two_with_big_ugly_name_to_discourage_you_from_using_it = 2;
(Edit: Don't mind the spaces in the names, they're inserted by the board.)