如何使用不同的静态值启动同一应用程序的两个实例?
问题:我想使用不同的参数运行我的应用程序,例如 app.exe -param1
,稍后我需要启动 app.exe -参数2
。参数来自实参。我需要将参数置于全局静态值中,以便能够随时从代码中的任何位置获取它。
怎么做呢?
我尝试过:
static QString gMyValues;
然后从 main.cpp
我做了一些事情:
::gMyValues = QString( argv[ argc - 1 ] );
然后从我试图获取的任何类中:
::gMyValues;
但没有运气,gMyValues 为空,但在乞求时它带有 arg 值。 .PS
。让它只是 int
的参数。
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的猜测是你对变量有多个定义。你的头文件中有这一行吗?
如果是这样,包含它的每个源文件都将拥有自己的
gMyValues
副本。并且只有main.cpp
中的那个会被填充正确的值。您应该像这样在头文件中声明它:
并在
main.cpp
中定义它:全局级别的
static
关键字并不意味着您认为它的作用。这意味着私人链接: http ://thesmithfam.org/blog/2010/06/12/the-many-meanings-of-the-c-static-keyword/My guess is you have more than one definition of the variable. Do you have this line in a header file?
If so, each and every source file that includes it will have its own copy of
gMyValues
. And only the one inmain.cpp
will be filled with correct value.You should declare it in the header file like this:
And define it in
main.cpp
: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/