CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2009
    Posts
    1,355

    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?

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    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)

  3. #3
    Join Date
    Apr 2009
    Posts
    1,355

    Re: is there any variable type that we can change it's value in global scope?

    Quote Originally Posted by 2kaud View Post
    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:

    Code:
    int a;
    a=100;
    i have seen something like these with 'static' variables

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    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)

  5. #5
    Join Date
    Apr 2009
    Posts
    1,355

    Re: is there any variable type that we can change it's value in global scope?

    Quote Originally Posted by 2kaud View Post
    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

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    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
  •  





Click Here to Expand Forum to Full Width

Featured