|
-
Dec 10th, 2003, 01:50 AM
#1
Thread Starter
Addicted Member
global variables
how do you declare global variables in c++ that can be used in any cpp file in the project?
To understand recursion, one must first understand the concept of recursion.
-
Dec 10th, 2003, 02:01 AM
#2
uhh.... declare it in a header file
-
Dec 10th, 2003, 03:15 AM
#3
Hyperactive Member
#include <stdio.h>
int a; /*This is a global variable */
main()
{}
-
Dec 10th, 2003, 06:28 AM
#4
Not NoteMe
And, for another cpp file, redeclare the variable, with 'extern' before it.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
-
Dec 10th, 2003, 11:49 AM
#5
Thread Starter
Addicted Member
Originally posted by sw_is_great
#include <stdio.h>
int a; /*This is a global variable */
main()
{}
no that's just for one module
To understand recursion, one must first understand the concept of recursion.
-
Dec 10th, 2003, 11:54 AM
#6
Thread Starter
Addicted Member
when i do the extern part, i get an 'already defined' error
To understand recursion, one must first understand the concept of recursion.
-
Dec 10th, 2003, 01:51 PM
#7
Not NoteMe
Don't put the variable in the header (unless the header's being included in all cpps). Just put it in oue of the cpps. With extern in all the others.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
-
Dec 10th, 2003, 04:21 PM
#8
Monday Morning Lunatic
What SLH said, with one addition.
If you put:...into a source file, you can put:
Code:
extern int g_myvar;
...into a header, which is included by all the other source files.
If you find yourself having a lot of global variables (you shouldn't really have *any* but I suppose sometimes you have to break the rules), put them all in one .c or .cpp file, with an associated header.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Dec 11th, 2003, 03:24 AM
#9
transcendental analytic
nothign wrong with global variables.. thats just oop nonsense, you can follow those rules if you are an oop fan, but C++ is not just for oop
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Dec 11th, 2003, 09:54 AM
#10
Frenzied Member
...but it just makes your code a hell of a lot easier to read if you
declare local variables where they're needed instead of having
globals.
Besides, what if you have a global that several functions
manipulate? If you have a big program, why would you want to
keep track of what ten or so functions manipulate a global and
when?
-
Dec 11th, 2003, 10:12 AM
#11
Member
hi,
For using a variable throught your project, declare it in a header
file and include the file wherever required.
The redefinition error is coming because of including a file more than once.
For avoiding this use macros like
#ifdef #def #ifndef #else
try this...
-
Dec 11th, 2003, 11:41 PM
#12
Originally posted by kedaman
nothign wrong with global variables.. thats just oop nonsense, you can follow those rules if you are an oop fan, but C++ is not just for oop
There is a problem with global variables. They can be used anywhere in anything. That is pretty dangerous.
Also, global variables have absolutely nothing to do with oop, so I got no idea why you mentioned it.
-
Dec 12th, 2003, 02:43 AM
#13
Frenzied Member
Originally posted by kasracer
There is a problem with global variables. They can be used anywhere in anything. That is pretty dangerous.
I totally agree.
Originally posted by kasracer
Also, global variables have absolutely nothing to do with oop, so I got no idea why you mentioned it.
I disagree here. Part of OOP is making a class or 'object'
for everything so you don't have to depend on globals.
Encapsulation, in theory, treats objects as 'black boxes' where
the data members and inner workings of the class are basically
invisible.
I'm not saying you CAN'T mix globals and OOP but the idea of OOP
is to do away with them.
-
Dec 12th, 2003, 02:52 AM
#14
Originally posted by wey97
I disagree here. Part of OOP is making a class or 'object'
for everything so you don't have to depend on globals.
Encapsulation, in theory, treats objects as 'black boxes' where
the data members and inner workings of the class are basically
invisible.
I'm not saying you CAN'T mix globals and OOP but the idea of OOP
is to do away with them.
Ugh, global variables don't have anything to do with OOP. OOP can be used to help eliminate them, but if a class eliminates a global variable, then there was no reason it should have been a global variable in the first place and could have been treated different.
Global variables are accessable everywhere. The fact that C++ has OOP doesn't change anything about global variables.
OOP creates a better way of managing a program for the programmer. It doesn't eliminate global variables in its nature. If OOP itself eliminates your global variables, then you probably need to re-think your programs design/structure.
-
Dec 12th, 2003, 03:24 AM
#15
transcendental analytic
There is a problem with global variables. They can be used anywhere in anything. That is pretty dangerous.
Dangerous for OOP programmers maybe, but anyone fairly experienced procedural programmer has the sense to avoid it. Global variables are there for a reason, and if you compare to java which just OOP and not multiparadigm like C++, there are no global variables at all.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Dec 12th, 2003, 03:42 AM
#16
Hyperactive Member
Originally posted by kedaman
Dangerous for OOP programmers maybe, but anyone fairly experienced procedural programmer has the sense to avoid it.
Isn't it a good idea to eliminate the risk anyway? It's like keeping a loaded gun with the safety off because your sure nobody is going to touch it.
Global variables are there for a reason, and if you compare to java which just OOP and not multiparadigm like C++, there are no global variables at all.
Aren't global variables just left overs from C (C++ being a superset of C and all)?
C.O.M.R.E.A.K.: Cybernetic Obedient Machine Responsible for Exploration and Accurate Killing
-
Dec 12th, 2003, 04:03 AM
#17
transcendental analytic
yes, and C is procedural. Eliminate the risk and eliminate its uses.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Dec 13th, 2003, 10:10 AM
#18
Even in C globals were kept to the minimum.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 15th, 2003, 08:48 AM
#19
Frenzied Member
Originally posted by kasracer
Ugh, global variables don't have anything to do with OOP. OOP can be used to help eliminate them, but if a class eliminates a global variable, then there was no reason it should have been a global variable in the first place and could have been treated different.
Global variables are accessable everywhere. The fact that C++ has OOP doesn't change anything about global variables.
OOP creates a better way of managing a program for the programmer. It doesn't eliminate global variables in its nature. If OOP itself eliminates your global variables, then you probably need to re-think your programs design/structure.
C++ is not complete OOP. That's why you can still have globals
and I can see cases where you might still need them or where it's
more useful to have them.
You're still missing the point.
Part of OOP is doing away with globals.
Why do you think there are no globals at all in Java?
That's because it's almost complete OOP.
-
Dec 15th, 2003, 10:55 AM
#20
Originally posted by wey97
Part of OOP is doing away with globals.
No it isn't
Originally posted by wey97
Why do you think there are no globals at all in Java?
You can use globals in Java as you can in C++......
-
Dec 15th, 2003, 11:10 AM
#21
Member
So how are we supposed to do it then?
Ok this is something that has baffled me for a while. Why not use global variables and what are the alternatives. I use global variables only when i need to but if there was a way round it that was safer then i would use it.
One of my current projects requires me to have a list of questions and i am using a STL vector to hold them all. This list needs to be accessed throughout my form (borland C++ builder) so how can i declare this list that is accessed in different functions to save/load/edit the list.
I have been told many times not to use global variables but i have never been told the alternative
Thanks
-
Dec 15th, 2003, 11:26 AM
#22
Re: So how are we supposed to do it then?
Originally posted by Greenslime
One of my current projects requires me to have a list of questions and i am using a STL vector to hold them all. This list needs to be accessed throughout my form (borland C++ builder) so how can i declare this list that is accessed in different functions to save/load/edit the list.
I have been told many times not to use global variables but i have never been told the alternative
Thanks
Pass by reference, pointers
-
Dec 15th, 2003, 11:36 AM
#23
Especially easy, just make it a member of the form class.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 15th, 2003, 11:38 AM
#24
Frenzied Member
Originally posted by kasracer
No it isn't
You can use globals in Java as you can in C++......
You're wrong, buddy.
http://www.developer.com/java/other/article.php/626151
Java and C++ are quite similar, and this makes it relatively easy for C++ programmers to learn the newer language. There are a few areas, however, that are different enough to cause problems, and one of these is the lack of global variables.
-
Dec 15th, 2003, 11:47 AM
#25
I don't know much of Java, but I can use global variables in it just like I can with C++ 
Maybe it means they can't be used across source files?
-
Dec 15th, 2003, 11:52 AM
#26
There are no global variables in Java, but you can give a class public static variables, the result is the same.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|