使用静态向量 [英] use of static vector

查看:100
本文介绍了使用静态向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hii,

如果我们使用静态向量变量,那么可以添加和删除该向量运行时的元素吗?请给我回答..

谢谢你....

hii,
If we use static vector variable then it is possible to add and remove the element of that vector runtime or not?? Please give me answer..
Thank u....

推荐答案

静态与否,没关系。它不能改变操作集,添加,删除任何东西。



静态对象通常很糟糕,或者它们只应在极少数情况下使用。它们不是在堆栈上分配的,它们不是每个声明类型实例的不同对象。如果某些类型声明了静态字段/属性,则每个进程中只有一个实例。无论您声明类型的对象有多少个实例,都只有一个静态字段/属性实例。如果声明类型的对象的实例数为零,则仍存在静态字段/属性。它是根据声明类型创建的,而不是按实例创建的。 (非静态成员也称为实例成员。)



他们有什么不好?首先,它们本身并不是线程安全的。要添加线程安全性,应该添加同步,例如,使用锁定。我是否甚至必须解释为什么非静态成员和本地(堆栈)变量可以在没有同步的情况下完成线程安全?这个想法是:它们可以是不同线程中的不同对象。只有当你需要共享对象时,你需要同步它们。



-SA
Static or not, it does not matter. It cannot change operation set, add, remove, anything.

Static objects are generally bad, or they should be used only in rare cases. They are not allocated on stack, they are not different objects per each instance of declaring type. If some type declared a static field/property, there is only one instance of in per process. No matter how many instances of the object of declaring type you have, there is only one instance of static field/property. If you have zero number of instances of the object of declaring type, a static field/property still exist. It is created per declaring type, not per instance. (Non-static members are also called instance members.)

What's bad with them? First of all, they are not inherently thread-safe. To add thread safety, synchronization should be added, say, by using a lock. Do I even have to explain why non-static members and local (stack) variables can be done thread safe without synchronization? The idea is: they can be different objects in different threads. Only when you need to share objects, you need to synchronize them.

—SA


这篇关于使用静态向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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