如何访问变量的先前值? [英] How Do I Access The Previous Values Of A Variable?

查看:79
本文介绍了如何访问变量的先前值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何访问变量的先前值?

考虑我已经初始化了x值,如下所示:

x = 5;

然后我设置x = 10;

和第三行我设置x = 20;

如何才能访问以前的x值?

how can i access the previous values of a variable?
consider i have initialized the x value like this:
x = 5;
then i set the x = 10;
and at the third line i set the x = 20;
how can i access the previous values of x ?

推荐答案

除非您将这些先前的值存储在其他位置,否则不能。

变量包含单个值,并且不保留不同的历史记录值已被保留。
You cannot, unless you have stored these previous values elsewhere.
A variable holds a single value, and does not maintain a history of the different values is has held.


它本身不存在于C ++或C#语言中。



做你想做的事,唯一的方法是手动构建一个记忆你的变量值的系统。



我看到的唯一实际用法是作为允许反向执行的调试器的一部分。
It doesn't exist natively in the language C++ or C#.

To do what you want, the only way is to manually build a system that memorize your variable values.

The only practical usage I see is as a part of a debugger that would allow backward execution.


你不能:内存被覆盖,以前的值丢失。

如果你需要以前的值,那么你必须明确地存储它们(例如使用集合,例如, std :: vector ,或一个普通数组。

你可以很好地将这种机制包装在你自己的'Variable'类中。
You cannot: memory is overwritten, previous values are lost.
If you need the previous values then you have to store them explicitely (e.g. using a collection, like for instance, a std::vector, or a plain array).
You may nicely wrap such a mechanism in your own 'Variable' class.


这篇关于如何访问变量的先前值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆