开发者

How to start two instances of the same app with different static values?

Problem: I want to run my application with the different arguments, for e开发者_C百科xample 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!


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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜