静态失去价值? [英] Static losing its value?
问题描述
我有一个带有一些静态变量的类:
MyClass.h中的
// -------------------------
lass MyClass {
....
public:
静态长myVals [4]; //声明
}
long MyClass :: myVals [4] = {0,0,0,0}; //定义
// ----------------------------
然后在我的程序的另一部分:(在This.cpp中)
MyClass :: myVals [1] = 123;
但后来当我尝试检索这个时:(在That.cpp中)
long x = MyClass :: myVals [1];
x总是等于零!?
一切都没有错误或警告编译,所以任何人都可以指出
为什么静态会失去它的价值?
谢谢
史蒂夫
Hi,
I have a class with some static variables:
in MyClass.h
//-------------------------
lass MyClass{
....
public:
static long myVals[4]; // declaration
}
long MyClass:: myVals[4] = {0,0,0,0}; //definition
//----------------------------
Then in another part of my program: (in This.cpp)
MyClass:: myVals[1] = 123;
But then later on when I try to retrieve this: (in That.cpp)
long x = MyClass:: myVals[1] ;
x always equals zero!?
Everything compiles with no errors or warnings, so can anyone point out
why the static is losing its value?
Thanks
Steve
推荐答案
*史蒂夫爱德华兹:
我有一个带有一些静态变量的类:
在MyClass.h中
// --------- ----------------
lass MyClass {
告诉我在哪里发送我的贡献,我会支持
运动使lass一个C ++关键字。
...
public:
static long myVals [4]; //声明
}
long MyClass :: myVals [4] = {0,0,0,0}; //定义
这是''lass''关键词的美妙之处:你不需要分号
你需要的其他地方他们使用''class''。
// ----------------------------
然后在我的程序的另一部分:(在This.cpp中)
MyClass :: myVals [1] = 123;
然后稍后当我尝试检索这个时:(在That.cpp中)
long x = MyClass :: myVals [1];
x总是等于零!?
这是一个很棒的Mysterie。
一切都没有错误或警告编译,所以任何人都可以指出
为什么静态正在失去它的价值?
Hi,
I have a class with some static variables:
in MyClass.h
//-------------------------
lass MyClass{
Just tell me where to send my contribution, and I''ll support the
movement to make "lass" a C++ keyword.
...
public:
static long myVals[4]; // declaration
}
long MyClass:: myVals[4] = {0,0,0,0}; //definition
This is the beauty of the ''lass'' keyword: you don''t need semicolons
where you''d otherwise need them using ''class''.
//----------------------------
Then in another part of my program: (in This.cpp)
MyClass:: myVals[1] = 123;
But then later on when I try to retrieve this: (in That.cpp)
long x = MyClass:: myVals[1] ;
x always equals zero!?
That''s a Great Mysterie.
Everything compiles with no errors or warnings, so can anyone point out
why the static is losing its value?
显然,这是拉扯你的弦的小姑娘。或者,看看线路
666.现在,这是一个不祥的数字,所以'肯定是问题
所在的位置。
如果这没有帮助,那么尝试发布一个展示
问题的最小程序。
Hth 。,
- Alf
-
答:因为它弄乱了人们正常的顺序阅读文字。
问:为什么这么糟糕?
A:热门帖子。
问:什么是最烦人的usenet和电子邮件中的东西?
Obviously, it''s the lass pulling your strings. Or, take a look at line
666. Now, that''s an ominous number, so that''s surely where the problem
is located.
If that doesn''t help, then try to post a minimal program that exhibits
the problem.
Hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
好的,我可能会要求(...即使没有错误或警告
表示这只是一个新闻组错字。)
class MyClass {
....
public:
静态长myVals [4]; //声明
};
long MyClass :: myVals [4] = {0,0,0,0}; //定义
我从Mysterie ...... 666中取出它吗?评论说不应该有问题,所以我其他地方一定有错误吗?
谢谢
Steve
OK, I probably asked for that (...even though "no errors or warnings"
would suggest it was a newsgroup-only typo.)
class MyClass{
....
public:
static long myVals[4]; // declaration
};
long MyClass:: myVals[4] = {0,0,0,0}; //definition
Do I take it from the "Mysterie...666" comment that there shouldn''t be
a problem and so I must have a mistake elsewhere?
Thanks
Steve
Steve555< fo ******* @ btinternet.com>写道:
Steve555 <fo*******@btinternet.com> wrote:
好吧,我可能会要求(...即使没有错误或警告
会表明这只是一个新闻组错字。)
类MyClass {
...
公共:
静态长myVals [4]; //声明
};
long MyClass :: myVals [4] = {0,0,0,0}; //定义
我从Mysterie ...... 666中取出它吗?评论说不应该成为一个问题,所以我必须在其他地方犯错误?
好吧,问题是,你给的代码:
然后在我的程序的另一部分:(在This.cpp中)
MyClass :: myVals [1] = 123;
但后来当我尝试检索它时:(在That.cpp中)
long x = MyClass :: myVals [1];
x总是等于零!?
OK, I probably asked for that (...even though "no errors or warnings"
would suggest it was a newsgroup-only typo.)
class MyClass{
...
public:
static long myVals[4]; // declaration
};
long MyClass:: myVals[4] = {0,0,0,0}; //definition
Do I take it from the "Mysterie...666" comment that there shouldn''t
be a problem and so I must have a mistake elsewhere?
Well, the problem is, that the code you gave:
Then in another part of my program: (in This.cpp)
MyClass:: myVals[1] = 123;
But then later on when I try to retrieve this: (in That.cpp)
long x = MyClass:: myVals[1] ;
x always equals zero!?
没有显示您的真实设置。将上面的
(没有明显的文本)粘贴到cpp中将无法编译,因此我们猜测
您的代码是什么样的。
通过''发布一个展示问题的最小程序''Alf告诉你
发布最小的*可能来源(包括#include',main,class
定义)仍然显示问题。这不包括任何定义,
函数等等,这些都不会影响问题。
hth
-
jb
(rot13的回复地址,先解读)
does not show anything about your real setup. Pasting the above
(without obvious text) into a cpp will not compile, thus making us guess
what your code looks like.
By ''post a minimal program that exhibits the problem'' Alf told you
to post the *tiniest* possible source (including #include''s, main, class
definitions) that still shows the problem. This excludes any defintions,
functions and whatnot, that do not influence the problem.
hth
--
jb
(reply address in rot13, unscramble first)
这篇关于静态失去价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!