我对C ++很陌生,在打印本地和全局变量时遇到了一些问题。考虑一下这段简单的代码:
z
我的目标是练习各种范围内变量的可访问性,然后打印它们。但是,我无法弄清楚为什么当我尝试打印最后一个变量2025
时,NetBeans会返回输出10
Global: 10
Local: 25
(in scope, before assignment) t = 10
This is another hidden scope!
z = 25
x = 28
(in scope, after re assignment) t = 28
Same scope of y. Look at code! z = 2520
RUN FINISHED; exit value 0; real time: 0ms; user: 0ms; system: 0ms
。
这是我的示例输出:
dn: cn=MyPolicy,ou=Policies,dc=XXX,dc=XXXX
cn: MyPolicy
objectClass: pwdPolicy
#objectClass: pwdPolicyChecker
objectClass: device
objectClass: top
pwdAttribute: 2.5.4.35
#pwdAttribute: userPassword
pwdMaxAge: 7862400
pwdExpireWarning: 6048000
pwdInHistory: 3
pwdCheckQuality: 2
pwdMinLength: 7
pwdMaxFailure: 3
pwdLockout: TRUE
pwdLockoutDuration: 300
pwdGraceAuthNLimit: 0
pwdFailureCountInterval: 0
pwdMustChange: TRUE
pwdAllowUserChange: TRUE
pwdSafeModify: FALSE
pwdReset: FALSE
希望有人能帮我理解发生的事情! :)
答案 0 :(得分:4)
不是 z 保持值 2520 是您在打印 z 和打印之间省略添加新行操作符的事实 m ...
你正在做的事情:cout << "Same scope of y. Look at code! z = " << z;
}
int m = 20;
cout << m;
但你应该这样做:
std::cout << "Same scope of y. Look at code! z = " << z << std::endl;
}
int m = 20;
std::cout << m << std::endl;
如果您只是遵循标记输出的标准并执行类似
的操作std::cout << "M is: "<<m << std::endl;
通过观察输出,您会更快地发现问题:
25M is: 20