Alternatives To Global Variables in C++

I need to set a variable in the main function and access it from a different function in the same file. I can not pass it to the function because it means changing the entire code structure, which is not an option. To avoid declaring a global variable I crated a namespace and I want to check if this is a good programming practice or is there a cleaner way to do it. This is the code:

namespace mylocalnamespace{
    int myglobalvar;
}

static void myFunc()
{
    ..... some code
    operationX(mylocalnamespace::myglobalvar);
    ..... some code

}
int main(int argc, char **argv)
{
   ..... some code
   mylocalnamespace::myglobalvar = atoi(argv[0]);
   ..... some code
}


from Recent Questions - Stack Overflow https://ift.tt/3jfUycJ
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation