c++ Initialization
c++ 的初始化过程比较复杂:根据对象的storage duration来分类。所谓storage duration是对象而言的,Storage duration is the property of an object that defines the minimum potential lifetime of the storage containing the object。跟lifetime密切相关,但是lifetime是具体的对象生存期。而storage duration是生存期的类型。一个是抽象的概念,一个是具体的概念,属于正交。
All non-local variables with static storage duration are initialized as part of program startup, before the execution of the main function begins. non-local 对象包括global variable, non-local static variable, namespace variable.
其中non local static duration的variable的初始化规则有:
1. zero Initialization:也就是初始化为0
2. const Initialization:如果是int i=4,那么在编译期就初始化了
以上都属于static Initialization初始化.
After all static initialization is completed, dynamic initialization of non-local variables occurs in the following situations:
int i=func()
那么non-loca static duration的variable变量呢,只有local scope的static 局部变量,它什么时候初始化?c++标准规定:
Variables declared at block scope with the specifier static
have static storage duration but are initialized the first time control passes through their declaration (unless their initialization is zero- or constant-initialization, which can be performed before the block is first entered). On all further calls, the declaration is skipped.
这就是惰性初始化
以上区分的关键点是static duration是否是non-local
那么根据external linkage 还是internal linkage 判断是否被不同的编译单元可见: global variable是有external的属性的。namespace的global variable也是。还有就是function函数默认是external的
- names of variables declared
extern
; - names of functions
那么常见的static修饰的变量和函数都是internal linkage的:
- variables, functions, or function templates declared
static
;
归纳static关键字的作用:
static class member static class function static object(non-local and local) static normal function
- // inside some .cpp file:
- static void foo(); // old "C" way of having internal linkage
- // C++ way:
- namespace
- {
- void this_function_has_internal_linkage()
- {
- // ...
- }
- }
c++ Initialization的更多相关文章
- Java:Double Brace Initialization
在我刚刚接触现在这个产品的时候,我就在我们的代码中接触到了对Double Brace Initialization的使用.那段代码用来初始化一个集合: final Set<String> ...
- initialization & finalization
Delphi 的pas文件中可以有initialization和finalization两个关键字, 1.initialization关键字: 在initialization关键字到finalizat ...
- 4.DB Initialization(数据库初始化)[EF Code-First系列]
前面的例子中,我们已经看到了Code-First自动为我们创建数据库的例子. 这里我们将要学习的是,当初始化的时候,Code-First是怎么决定数据库的名字和服务的呢??? 下面的图,解释了这一切! ...
- Linux 克隆虚拟机引起的“Device eth0 does not seem to be present, delaying initialization”
虚拟机Vmware上克隆了一个Red Hat Enterprise Linx启动时发现找不到网卡,如下所示,如果你在命令窗口启动网络服务就会遇到"Device eth0 does not s ...
- Resource Acquisition Is Initialization(RAII Idiom)
原文链接:http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Resource_Acquisition_Is_Initialization Intent ...
- Swift 提示:Initialization of variable was never used consider replacing with assignment to _ or removing it
Swift 提示:Initialization of variable was never used consider replacing with assignment to _ or removi ...
- TIJ——Chapter Five:Initialization & Cleanup
Method overloading |_Distinguishing overloaded methods If the methods hava the same name, how can Ja ...
- linux 网卡问题 Device eth0 does not seem to be present,delaying initialization.
Device eth0 does not seem to be present,delaying initialization. 网上搜索后才发现原因所在:原来vmware在复制了虚拟机后会自动生成一 ...
- Device eth0 does not seem to be present, delaying initialization. 问题
今天在复制vmware的时候 出现网卡无法启动 报错显示 Device eth0 does not seem to be present, delaying initialization. 这个错误原 ...
- “ORA-01033:ORACLE initialization or shutdown in progress”错误的解决
网页上显示以下错误信息: ORA-:ORACLE initialization or shutdown in progress 启动oracle数据库,有以下提示信息 Database mounted ...
随机推荐
- Thinking in 查询设计
近日,互联网动物园的各位小伙伴们召开了一次会议,考虑到大火的电子商务,准备在动物园里开发一个电商系统.首先上台的是销售山鸡,清了清嗓子,说道,人类正在进行电商革命,动物园也需要上一个电商系统,必要性有 ...
- C++ std::unordered_map
std::unordered_map template < class Key, // unordered_map::key_type class T, // unordered_map::ma ...
- IPMI命令
yum -y install ipmitool/etc/init.d/ipmi start ipmitool -I open lan set 1 ipaddr 172.16.8.33ipmitool ...
- wireshark问题
上一篇wireshark编译成功了,生成了相应的wireshark.exe,dumpcap.exe等可执行文件(这些文件都是可以运行的),编译工具用的是VS2010,但是新生成的文件和文件夹中没有找到 ...
- Demo—标题左右两侧的对等横线
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- SQL server 累加求和
1. SELECT SalesOrderID, ProductID, OrderQty ,SUM(OrderQty) OVER(PARTITION BY SalesOrderID) AS Tot ...
- [Delphi] Webbroker ISAPI 示例说明
新建Webbroker项目: 选择类型: 开始可以使用:Indy VCL Application 方便调试,完成后,再新建一个DLL 项目,引用业务单元. 示例代码如下: unit uDataMo ...
- 《T-SQL查询》- SQL逻辑处理
下面列出SQL查询语句的一般形式,以及各个子句被逻辑处理的顺序步骤: (8) SELECT (9) DISTINCT (11) <TOP_specification> <select ...
- GitHub上创建组织
4.3. 组织和团队 GitHub 在早期没有专门为组织提供账号,很多企业用户或大型开源组织只好使用普通用户账号作为组织的共享账号来使用.后来,GitHub推出了组织这一新的账号管理模式,满足大型开发 ...
- robot中使用evaluate转化数据格式
如果你使用robot却没有用过evaluate,那你将永远禁锢在框架中. json对象格式入参可以使用字典格式直接传入,但最近有一个接口测试的入参是一个json数组,在传参时总是提示请求参数不合法, ...