静态物体的作用 [英] role of static objects

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

问题描述

我遇到了一个C ++代码,该代码创建了一个类的静态对象.

I came across a c++ code that creates static objects of a class.

class X {
int i;
public:
X(int ii = 0) : i(ii) {} // Default
~X() { cout << "X::~X()" << endl; }
};
void f() {
static X x1(47);
static X x2; // Default constructor required
}
int main() {
f();
} .



我有几个问题
1.静态对象有什么用?
2.由于静态不在类范围内,我们如何使用静态对象调用类的函数?



I have a few questions
1.What is the use of static objects?
2.Since static is not in class scope ,how can we call functions of a class using static objects?

推荐答案

静态对象在某些情况下很有用.例如,当您实现单例模式(只能创建一个类的一个对象)时,可以使用如下所示的静态对象
static objects are useful in some scenarious. for example when u implement singleton pattern(where only one object of a class can be created) you can use static object like below
class A
{
   private:
   A() {}
   public:
   static A* GetInstance()
   {
     static A a;
     return &a;
   }
}



希望这是有用的
jkchan
http://cgmath.blogspot.com



hopes this is useful
jkchan
http://cgmath.blogspot.com


在您的代码中,对象是 local 静态变量(例如,请参见此处 [ ^ ]).
那就是x1x2的构造函数(和析构函数)在每个应用程序执行中被调用一次,无论函数f被调用多少次.此外,根据文档所述,对象在调用f之间保留其状态.
In your code the objects are local static variables (see, for instance, here[^]).
That is the constructors (and destructors) of x1 and x2 are called once per application execution, no matter how many times the function f is called. Moreover, has stated by the documentation, the objects retains their states between calls to f.


尽管问题#1有意义,但#2表明您不了解某些内容关于静态的,但是我不明白是什么.您能解释一下您的担忧吗?您是否看到类的静态成员与代码中显示的静态本地对象之间的区别?您为什么认为使用它们可能会有问题?

您应该了解,不是在堆栈或堆上创建每个这样的对象,而是在每个进程的运行时在某个常见的固定位置仅创建一次.也就是说,对象x1x2仅初始化一次;它们位于进程内存的相同内存位置,即使您再次调用f.

这意味着您可以例如在每次调用之间将静态对象中的值累加.从语法的角度来看,这两个对象是f的局部对象,但是从内存布局的角度来看,它们在外部,位于为项目的静态数据保留的某个固定内存位置.

不清楚您对使用该类的方法有何关注.哪些方法?您只需要考虑以上注意事项;电话本身就是……只是电话,没什么特别的.

—SA
While the question #1 makes sense, #2 shows that you don''t understand something about static, but I cannot understand what. Could you explain your concern? Do you see the difference between static members of class and static local objects you show in your code? Why do you think using them could be a problem?

You should understand that each such object is not created on stack or heap but is created only once per the process''s runtime in some common fixed location. That is, the objects x1 and x2 are initialized only once; they are located in the same memory location of the process''s memory, even if you call f again.

It means that you can, for example, accumulate the values in the static object from call to call. From the standpoint of syntax, these two objects are local to f, but from the standpoint of memory layout, they are outside, in some fixed memory location reserved for the static data of the project.

It is not clear what is your concern about using the class''s methods. Which methods? You just need to take into account the above considerations; the calls themselves are… just calls, nothing special.

—SA


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

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