我如何获得随机访问... [英] How Do I Get A Random Access To ...

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

问题描述

我有一个变量列表(教授),我希望随机访问这些变量,并在获取访问权限后将其从列表中删除

解决方案

< blockquote>你总是可以随机访问某些范围内定义的一些变量集,当你将堆栈框架放在定义它们的位置时,你总是会松开这些变量。



也许你的意思是别的,比如班级成员。但那一切都取决于他们是什么。如果这些是静态成员,则无法删除它们。如果它们是实例成员,则属于某个类的特定实例,因此只要删除该实例,就会丢失所有实例成员及其值。因此,它为您提供了第一个选项:您使用某个实例和某些成员(字段),然后以某种方式使用它们的值,比如将值复制到相同或其他类的其他实例(您可以将它们保持为只读在初始化它们或第二个实例之后),然后你删除你使用的实例。



类字段的另一个选项是:使它们非公开,并通过访问器方法以间接方式提供公共(朋友?)访问权限。在最简单的形式中,对于字段类型T,它可以是 T get *()和void set *(T)实例方法。 (我使用'*'符号作为通配符作为名称,而不是指针在这里:-)。)对于每个实例成员,您只能有 getter ,或者只有 setter ,或两者兼备,具体取决于您的目的。显然,访问者可以为你的字段分配和读取操作添加一些副作用



(顺便说一下,看看这个有趣的CodeProject文章讨论C ++的类似C#的属性,它可以让你明白这个概念: C ++ 11 Properties [ ^ ]。)



现在,这个副作用可能是阻止访问赋值给字段或读取它,在你的实例进入下一个摆脱使用成员状态之后。我希望实现这种效果应该是显而易见的。



-SA


i have a list of variables (professor), and i want to get a random access to this variables and remove it from the list after getting access to it

解决方案

You always have random access to some set of variables defined in some scope, and you always loose these variables when you leave the stack frame where they are defined.

Perhaps you mean something else, such as class members. But then it all depends on what are they. If these are static members, you cannot remove them. If they are instance members, the belong to a particular instance of a class, so as soon as you delete the instance, you loose all instance members and its values. So, it gives you the first option: you work with some instance and some members (fields) and then use their values somehow, say, copy the values to some other instance of the same or another class (you can keep them read-only after initialization of them or the second instance), and then you delete the instance you worked with.

Another option with class fields is this: make them non-public, and provide public (friend?) access to them in the indirect fashion, through accessor methods. In its simplest form, for the field type T, it can be T get*() and void set*(T) instance methods. (I use '*' symbol as a wildcard for a name, not as pointer here :-).) Per each instance member, you can have only getter, or only setter, or both, depending on your purpose. Apparently, the accessors can add some side effect to your field assignment and read operations.

(By the way, see this interesting CodeProject article discussing C#-like properties for C++, it can make this concept clear to you: C++11 Properties[^].)

Now, this side-effect could be blocking access to assignment a value to a field or reading it, after your instance enters next "get rid of used members" state. I hope implementation of such effect should be obvious.

—SA


这篇关于我如何获得随机访问...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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