CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2006
    Posts
    37

    Smile Restrict usage of Global variable in function

    Hi all,

    Suppose i have a globale variable for some purpose.

    And i want that a perticular function should not use the global variable.

    Probabily i declare the global variable for myself but it is exposed to others too.

    I want instead of me or function i decide the other function can not use that.

    int i =10;

    int func1() //written by me
    {
    i = i + 1;
    return i;
    }

    int func2() //written by others
    {
    i = i + 1; //restrict this usage of global variable
    return i;
    }


    Is there is any way to stop from others (functions) to use it.

    Your comment is highly appreciable...

    Thanks.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Restrict usage of Global variable in function

    Quote Originally Posted by bidesh
    Hi all,

    Suppose i have a globale variable for some purpose.

    And i want that a perticular function should not use the global variable.

    Probabily i declare the global variable for myself but it is exposed to others too.

    I want instead of me or function i decide the other function can not use that.
    Then you either not use global variables (which is not recommended for C++ programs anyway), or you put a big comment in your code:
    Code:
    // DON'T USE THIS GLOBAL VARIABLE!!!!
    Otherwise, nothing stops another programmer from using your global variable for whatever purposes they wish.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: Restrict usage of Global variable in function

    If you and your colleagues are writing codes in different files, you can use static variable declared at file scope. Static variable declared at file scope can only be accessed by all functions within the same file but not in other files.
    quoted from C++ Coding Standards:

    KISS (Keep It Simple Software):
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.

    Avoid magic number:
    Programming isn't magic, so don't incant it.

  4. #4
    Join Date
    Nov 2007
    Posts
    51

    Re: Restrict usage of Global variable in function

    Quote Originally Posted by Kheun
    If you and your colleagues are writing codes in different files, you can use static variable declared at file scope. Static variable declared at file scope can only be accessed by all functions within the same file but not in other files.
    Alternatively, you can use anonymous namespace. Variable "i" cannot be accessed outside the file.

    Code:
    namespace {
      int i;
    }
    
    int main() {
      ++i;
      return 0;
    }
    If you want to allow access to the variable from multiple files, but do not want other developers to accidentally access the variable, define a namespace and declare your variable there. The following is an example.

    Code:
    // mns.h
    // declare i
    namespace MNS {
      extern int i;
    }
    Code:
    // mns.cpp
    // define i
    #include "mns.h"
    int MNS::i;
    Code:
    // main.cpp
    #include "mns.h"
    
    using namespace MNS;
    
    int main() {
      ++i;// no need to specify namespace here
      MNS::i--;// but you can specify, if you want to
      return 0;
    }

  5. #5
    Join Date
    Dec 2006
    Posts
    37

    Re: Restrict usage of Global variable in function

    Thanks paul, Kheun, phl for the quick reply...

    I namespace idea may work for me i hope.

    I would remember not to use global variable again in future.

    Thank you.

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