[英]Why can't I assign this int a value?
I have a class: 我有一堂课:
class A
{
public:
A();
~A();
void IncrementNum();
private:
int num;
};
A::A() : num(0)
{
}
A::~A()
{
}
void A::IncrementNum()
{
num++;
}
int main()
{
A obj;
obj.IncrementNum();
}
When I set a breakpoint in the constructor, it shows that num is equal to some random value (such as -2483290483), which I take as meaning it's unassigned. 当我在构造函数中设置断点时,它表明num等于某个随机值(例如-2483290483),我认为这是未分配的。 And sure enough, when I call IncerementNum(), and set a breakpoint on the line after
num++
, it shows the exact same thing (num equals some random number). 可以肯定的是,当我调用IncerementNum()并在
num++
之后的行上设置断点时,它显示的是完全相同的东西(num等于某个随机数)。 Repeated calls to IncrementNum() do not change anything, num doesn't change. 重复调用IncrementNum()不会更改任何内容,num不会更改。
So I decided to instead change num++
to num = 1
, thinking surely this would force num
to be set. 因此,我决定改为将
num++
更改为num = 1
,肯定会强制设置num
。 Nope. 不。
num
still shows as being some random number even after discretely setting it to 1. Again, successive calls to the new version of IncrementNum() fail to change its value. 即使将
num
离散地设置为num
仍然显示为某个随机数。再次,对新版本的IncrementNum()的连续调用无法更改其值。
Any idea what could be causing this? 知道是什么原因造成的吗?
Other info: 其他资讯:
I'm using Windows 7 Home Edition and Visual Studio 2010 我正在使用Windows 7家庭版和Visual Studio 2010
You're probably compiling it in Release mode. 您可能正在发布模式下进行编译。 The Compiler sees that
num
is not being used anywhere and optimized it out. 编译器发现
num
未被任何地方使用,并对其进行了优化。 Try to display num
with printf()
or cout
and try the test again. 尝试使用
printf()
或cout
显示num
,然后重试测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.