Globals and namespace
-
I am using some object refrence and boolean variables, these are declared golbally. I know this isn't a good practice but, it is required. I want to know Can i use nampespace to store these globals and is it of any importance. Also I don't know how to create and use namespace. Can you help me plz.
-
I am using some object refrence and boolean variables, these are declared golbally. I know this isn't a good practice but, it is required. I want to know Can i use nampespace to store these globals and is it of any importance. Also I don't know how to create and use namespace. Can you help me plz.
yes, you can put them in a namespace, and it's good style to do so. to declare:
namespace myNameSpace
{
bool globalBool;
CMyObject globalObject;
...
};... to use:
myNameSpace::globalBool = false;
-
I am using some object refrence and boolean variables, these are declared golbally. I know this isn't a good practice but, it is required. I want to know Can i use nampespace to store these globals and is it of any importance. Also I don't know how to create and use namespace. Can you help me plz.
-
I am using some object refrence and boolean variables, these are declared golbally. I know this isn't a good practice but, it is required. I want to know Can i use nampespace to store these globals and is it of any importance. Also I don't know how to create and use namespace. Can you help me plz.
Just a little addition to previous replies , if you don't want to write
myNameSpace::SomeFunction()
every time you call it, then you can add following line to yourmain()
function :using namespace myNameSpace;
And then you will be able to directly callSomeFunction()
inmain();
"Success is the ability to go from one failure to another with no loss of enthusiasm." - W.Churchill
-
I am using some object refrence and boolean variables, these are declared golbally. I know this isn't a good practice but, it is required. I want to know Can i use nampespace to store these globals and is it of any importance. Also I don't know how to create and use namespace. Can you help me plz.
karmendra_js wrote: Also I don't know how to create and use namespace.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV
-
I am using some object refrence and boolean variables, these are declared golbally. I know this isn't a good practice but, it is required. I want to know Can i use nampespace to store these globals and is it of any importance. Also I don't know how to create and use namespace. Can you help me plz.
Hi, Yes you can locate global variables in namespaces. The importance of doing so depends on the specific case. For example, if you are afraid your var's name could conflict with another var's name, put yours in a namespace. Another example, if you have many global variables (hope you don't) you'd rather put them in a namespace in terms of 'well organization'. Code: namespace Globals { bool bVar1; bool bVar2; // any others... } // namespace Globals Now let's use it: // method 1. (not preferred) using namespace Globals; void f() { bVar1 = false; } // method 2. (less preferred) using Global::bVar1 void f() { bVar1 = false; } // method 3. (ok) void f() { Globals::bVar1 = false; }