Reputation: 12391
Problem: I want to run my application with the different arguments, for example app.exe -param1
and a little bit later I need to start app.exe -param2
. Parameters comes from arguments. Arguments I need to place to global static value, to be able to get it at any time from any where in code.
How to do that?
I have tried:
static QString gMyValues;
then from main.cpp
I do something:
::gMyValues = QString( argv[ argc - 1 ] );
and then from any class I'm trying to get:
::gMyValues;
but no luck, gMyValues empty, but at the begging it was with arg value...
PS. Let it be only int
's params.
Thanks!
Upvotes: 0
Views: 67
Reputation: 12832
My guess is you have more than one definition of the variable. Do you have this line in a header file?
static QString gMyValues;
If so, each and every source file that includes it will have its own copy of gMyValues
. And only the one in main.cpp
will be filled with correct value.
You should declare it in the header file like this:
extern QString gMyValues;
And define it in main.cpp
:
QString gMyValues;
The static
keyword in global level doesn't mean what you think it does. It means private linkage: http://thesmithfam.org/blog/2010/06/12/the-many-meanings-of-the-c-static-keyword/
Upvotes: 4