-
November 30th, 2007, 01:16 AM
#1
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.
-
November 30th, 2007, 01:48 AM
#2
Re: Restrict usage of Global variable in function
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
-
November 30th, 2007, 01:54 AM
#3
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.
-
November 30th, 2007, 02:35 AM
#4
Re: Restrict usage of Global variable in function
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;
}
-
November 30th, 2007, 07:56 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|