C ++中类静态变量的生存期是多少? [英] What is the lifetime of class static variables in C++?

查看:133
本文介绍了C ++中类静态变量的生存期是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个名为Test ::的课程,

If I have a class called Test ::

class Test
{
    static std::vector<int> staticVector;
};

staticVector 何时构造,何时破坏?

when does staticVector get constructed and when does it get destructed ?

是用Test类的第一个对象的实例化,还是像常规静态变量一样?

Is it with the instantiation of the first object of Test class, or just like regular static variables ?

为澄清起见,在阅读了《编程语言概念》(Sebesta Ch-5.4.3.1)之后,我想到了这个问题,并说了::

请注意,当静态修饰符 出现在 C ++中的类定义中的变量, Java和C#,与它无关 变量的生存期.在那里面 上下文,它意味着变量是一个 类变量,而不是 实例变量.多种用途 保留字可能会造成混淆 特别是对于那些学习 语言.

Note that when the static modifier appears in the declaration of a variable in a class definition in C++, Java and C#, it has nothing to do with the lifetime of the variable. In that context, it means the variable is a class variable, rather than an instance variable. The multiple use of a reserved word can be confusing particularly to those learning the language.

您了解吗? :(

推荐答案

与常规静态(全局)变量完全一样.

Exactly like regular static (global) variables.

这篇关于C ++中类静态变量的生存期是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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