how do you declare global variables in c++ that can be used in any cpp file in the project?
Printable View
how do you declare global variables in c++ that can be used in any cpp file in the project?
uhh.... declare it in a header file
#include <stdio.h>
int a; /*This is a global variable */
main()
{}
And, for another cpp file, redeclare the variable, with 'extern' before it.
no that's just for one moduleQuote:
Originally posted by sw_is_great
#include <stdio.h>
int a; /*This is a global variable */
main()
{}
when i do the extern part, i get an 'already defined' error
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.
What SLH said, with one addition.
If you put:...into a source file, you can put:Code:int g_myvar;
...into a header, which is included by all the other source files.Code:extern int g_myvar;
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.
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
...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?
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...
There is a problem with global variables. They can be used anywhere in anything. That is pretty dangerous.Quote:
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
Also, global variables have absolutely nothing to do with oop, so I got no idea why you mentioned it.
I totally agree.Quote:
Originally posted by kasracer
There is a problem with global variables. They can be used anywhere in anything. That is pretty dangerous.
I disagree here. Part of OOP is making a class or 'object'Quote:
Originally posted by kasracer
Also, global variables have absolutely nothing to do with oop, so I got no idea why you mentioned it.
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.Quote:
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.
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.
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.Quote:
There is a problem with global variables. They can be used anywhere in anything. That is pretty dangerous.
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.Quote:
Originally posted by kedaman
Dangerous for OOP programmers maybe, but anyone fairly experienced procedural programmer has the sense to avoid it.
Aren't global variables just left overs from C (C++ being a superset of C and all)?Quote:
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.
yes, and C is procedural. Eliminate the risk and eliminate its uses.
Even in C globals were kept to the minimum.
C++ is not complete OOP. That's why you can still have globalsQuote:
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.
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.
No it isn'tQuote:
Originally posted by wey97
Part of OOP is doing away with globals.
You can use globals in Java as you can in C++......Quote:
Originally posted by wey97
Why do you think there are no globals at all in Java?
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 :wave:
Pass by reference, pointersQuote:
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 :wave:
Especially easy, just make it a member of the form class.
You're wrong, buddy.Quote:
Originally posted by kasracer
No it isn't
You can use globals in Java as you can in C++......
http://www.developer.com/java/other/article.php/626151
Quote:
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.
I don't know much of Java, but I can use global variables in it just like I can with C++ :confused:Quote:
Originally posted by wey97
You're wrong, buddy.
http://www.developer.com/java/other/article.php/626151
Maybe it means they can't be used across source files?
There are no global variables in Java, but you can give a class public static variables, the result is the same.