How can I define global arrays, vars, etc inside a function? I want to access these objects from other .cpp files.
How can I define global arrays, vars, etc inside a function? I want to access these objects from other .cpp files.
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
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?
Last edited by Kurisu; 02-06-2006 at 05:50 AM. Reason: Curious if signature is working.
Don't ever declare global variables. If a function needs a variable, then pass it to the function.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.
Last edited by siavoshkc; 02-06-2006 at 07:13 AM.
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
Forum displayed wrong last post.
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
The caller is also a function...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?
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
My question was simple guys. Defining Global inside a block.
Is there any way to do so, or not?
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
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.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
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.
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
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.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
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.
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
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 saw a sign that said 'Drink Canada Dry', so I started"
-- Brendan Behan
Free Compiler: Visual C++ 2005 Express
If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?
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.)
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law