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 ...
随机推荐
- HTTP/2协议
一.简介 http://toutiao.com/a6258917202906104066/
- p2944 [USACO09MAR]地震损失2Earthquake Damage 2
传送门 分析 我们让s到1,关键点到t分别连流量为inf的边 于是我们可以考虑跑s到t的最小割 于是我们将所有点拆为两个点,关键点和1的两个点之间连inf,其余点连1 将原图的边也连上,流量为inf ...
- 开源项目spring-shiro-training思维导图
写在前面 终于完成了一个开源项目的思维导图解读.选spring-shiro-training这个项目解读是因为它开源,然后涉及到了很多我们常用的技术,如缓存,权限,任务调度,ssm框架,Druid监控 ...
- eclipse Multiple annotations found at this line
参考:http://blog.csdn.net/li396864285/article/details/42745071 这样的错不影响编程,是eclipse的校验问题 去掉相应的Build就行了
- charCodeAt方法以及Unicode中文汉字编码范围
js的charCodeAt() 方法可返回指定位置的字符的 Unicode 编码.这个返回值是 0 - 65535 之间的整数. 在字符串 "Hello world!" 中,我们将 ...
- 金士顿DT100 G3 PS2251-07海力士U盘量产修复成功教程
金士顿DT100 G3这款U盘.用了大概一年半没什么问题,前段时间拷贝大文件,无奈电脑突然断电,然后这个U盘在电脑上就读不出来了,只能显示盘符,所以有了修复过程. 最开始我也是菜鸟,不知道怎么搞, ...
- TinyMCE3.x整合教程-Xproer.WordPaster
版权所有 2009-2017 荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webplug/wordpa ...
- 编写高质量代码改善C#程序的157个建议——建议91:可见字段应该重构为属性
建议91:可见字段应该重构为属性 字段和属性的本质区别就是属性是方法. 查看下面这个Person类型: class Person { public string Name { get; set; } ...
- 命令行执行 mvn package 和常见mvn命令
cmd 打开命令提示符, 然后打开代码所在目录,例如 d: cd d:/code 执行 mvn package Maven常用命令: 1. 创建Maven的普通java项目: mvn arc ...
- springDao的jdbctemplate
pom文件 <?xml version="1.0" encoding="UTF-8"?><project xmlns="http:/ ...