![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2005
Posts: 4
Rep Power: 0
![]() |
C++ Global Variables question
Hey, Im learning about global variables and i have a question concerning the example code below. Now I know that count is declared outside any function therefore can be used by any, but the value of count is given in the int main() function, so when it calls the second function how is it that it still works ? or is it that it doesnt matter where the value given is located as long as the variable is declared globaly or as long as there isnt a local variable with the same name that will interfere ?
thanks in advanced #include <iostream>
using namespace std;
void func1();
int count; // this is a global variable
int main()
{
int i; // this is a local variable
for(i=0; i<10; i++) {
count = i * 2;
func1();
}
return 0;
}
void func1()
{
cout << "count: " << count; // access global count
cout << '\n'; // output a newline
} |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
That will work fine, but yes, you're right - you can't declare a local variable if you have a global variable with the same name.
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: May 2005
Posts: 4
Rep Power: 0
![]() |
ok thanks , now I got a second question about it ..
what if u were to declare a global variable named var for instance. Now lets say you have 2 functions .. can u use var in both functions with each function having a different value for it .. say : outside all functions: int var; inside the first function : var = 10; inside the second function : var = 50; |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 317
Rep Power: 4
![]() |
Yes, variables in different functions don't clash with each other.
If you have a global variable of a given name, for example foo, you can create a local one with the same name if you please. int foo;
int bar() {
int foo;
}Code inside the function, that is in the same scope as the local variable foo, will refer to the local variable when it refers to the variable called foo. Code outside it, in other functions, will refer to the global foo, unless they define a variable called foo of their own. -- I just realised I kind of missed the point of what you were asking there. If you declared the global variable var, you could use it in two different functions, and they'd both be referring to the same variable. So in func1, if you set var to 10, then call func2, func2 will see the value 10 in it. If it now sets it to 20 and returns, and you then call func1, func1 sees the new value. A global variable is one name and one storage location and it's shared throughout the program. It can only take one value at a time, and any function that assigns a value to the global variable will change the value any other function sees. Last edited by mackenga; May 19th, 2005 at 4:35 PM. |
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
I am not sure about c++ but as a rule of thumb when i am using java i tend to use this.<variable> for gloadls and then just the name of the variable for the local one.
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity." - Albert Einstein |
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 317
Rep Power: 4
![]() |
There aren't any global variables in Java. As well as not being inside a function, global variables (in languages that have them) are not inside anything else.
|
|
|
|
|
|
#7 |
|
Programming Guru
![]() |
well its close enough to a global variable.
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity." - Albert Einstein |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Mar 2005
Posts: 27
Rep Power: 0
![]() |
You can use the same name over and over in functions and blocks. C/C++ has a means of hiding the hierarchically higher variables.
For instance: int a;
void f () { int a; { int a; { int a; } } }Global versions of variables can be accessed through ::my_var where my_var is the name of the variable. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|