-
November 17th, 2013, 07:27 AM
#1
is there any variable type that we can change it's value in global scope?
is there any variable type(or with another keyword) that we can change it's value in global scope?
-
November 17th, 2013, 08:36 AM
#2
Re: is there any variable type that we can change it's value in global scope?
I'm not sure I'm understanding the question. Any non-const variable defined as being in global scope in a single program file can be changed anywhere in that program file after the definiton providing the variable hasn't been re-defined within a local block.
Do you mean that you want a global variable defined in one program file to be able to be accessed in another program file that is part of the same compilation project/solution?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 17th, 2013, 08:46 AM
#3
Re: is there any variable type that we can change it's value in global scope?
Originally Posted by 2kaud
I'm not sure I'm understanding the question. Any non-const variable defined as being in global scope in a single program file can be changed anywhere in that program file after the definiton providing the variable hasn't been re-defined within a local block.
Do you mean that you want a global variable defined in one program file to be able to be accessed in another program file that is part of the same compilation project/solution?
example:
i have seen something like these with 'static' variables
-
November 17th, 2013, 08:57 AM
#4
Re: is there any variable type that we can change it's value in global scope?
Static variables are most commonly used in a function. The variable defined as static is local to the function but retains its value between each call to the function.
Code:
void fun1()
{
static int cnt = 0;
cout << "cnt value is " << cnt++ << endl;
}
Every time you call fun1, cnt is incremented even though it is local to the function. See
http://msdn.microsoft.com/en-us/libr...vs.110%29.aspx
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 17th, 2013, 09:02 AM
#5
Re: is there any variable type that we can change it's value in global scope?
Originally Posted by 2kaud
Static variables are most commonly used in a function. The variable defined as static is local to the function but retains its value between each call to the function.
Code:
void fun1()
{
static int cnt = 0;
cout << "cnt value is " << cnt++ << endl;
}
Every time you call fun1, cnt is incremented even though it is local to the function. See
http://msdn.microsoft.com/en-us/libr...vs.110%29.aspx
sorry but outside the function
-
November 17th, 2013, 09:47 AM
#6
Re: is there any variable type that we can change it's value in global scope?
Like this?
Code:
int a;
void f1()
{
a = 2;
}
int main()
{
a = 1;
f1();
cout << "a " << a << endl;
return 0;
}
static used at the global level simply means that the variable can't be used outside of that 'code unit'.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
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
|