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的更多相关文章

  1. Java:Double Brace Initialization

    在我刚刚接触现在这个产品的时候,我就在我们的代码中接触到了对Double Brace Initialization的使用.那段代码用来初始化一个集合: final Set<String> ...

  2. initialization & finalization

    Delphi 的pas文件中可以有initialization和finalization两个关键字, 1.initialization关键字: 在initialization关键字到finalizat ...

  3. 4.DB Initialization(数据库初始化)[EF Code-First系列]

    前面的例子中,我们已经看到了Code-First自动为我们创建数据库的例子. 这里我们将要学习的是,当初始化的时候,Code-First是怎么决定数据库的名字和服务的呢??? 下面的图,解释了这一切! ...

  4. Linux 克隆虚拟机引起的“Device eth0 does not seem to be present, delaying initialization”

    虚拟机Vmware上克隆了一个Red Hat Enterprise Linx启动时发现找不到网卡,如下所示,如果你在命令窗口启动网络服务就会遇到"Device eth0 does not s ...

  5. Resource Acquisition Is Initialization(RAII Idiom)

    原文链接:http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Resource_Acquisition_Is_Initialization Intent ...

  6. 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 ...

  7. TIJ——Chapter Five:Initialization & Cleanup

    Method overloading |_Distinguishing overloaded methods If the methods hava the same name, how can Ja ...

  8. linux 网卡问题 Device eth0 does not seem to be present,delaying initialization.

    Device eth0 does not seem to be present,delaying initialization. 网上搜索后才发现原因所在:原来vmware在复制了虚拟机后会自动生成一 ...

  9. Device eth0 does not seem to be present, delaying initialization. 问题

    今天在复制vmware的时候 出现网卡无法启动 报错显示 Device eth0 does not seem to be present, delaying initialization. 这个错误原 ...

  10. “ORA-01033:ORACLE initialization or shutdown in progress”错误的解决

    网页上显示以下错误信息: ORA-:ORACLE initialization or shutdown in progress 启动oracle数据库,有以下提示信息 Database mounted ...

随机推荐

  1. 785. Is Graph Bipartite?从两个集合中取点构图

    [抄题]: Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is  ...

  2. 单例模式的N种写法

    单例模式算是设计模式中最容易理解,也是最容易手写代码的模式了吧.但是其中的坑却不少,所以也常作为面试题来考.本文主要对几种单例写法的整理,并分析其优缺点.很多都是一些老生常谈的问题,但如果你不知道如何 ...

  3. [Cookie] Read Cookie and Pass in headers

    在同一个Suite里 import com.eviware.soapui.support.types.StringToStringMap def headers = testRunner.testCa ...

  4. echo $[1 + 2] shell中 $[] 在bash中同$(()),用于算术计算

    shell脚本编写:echo $[ 11#8+1] 输出结果是几,为什么,怎么算来的?  摘自:https://zhidao.baidu.com/question/334766451.html   结 ...

  5. 在Lua中封装一个调试日志(附lua时间格式)

    --自己封装一个Debug调试日志 Debug={} Info={} local function writeMsgToFile(filepath,msg) end function Debug.Lo ...

  6. Alpha冲刺 - (8/10)

    队名:彳艮彳亍团队 组长博客:戳我进入 作业博客:班级博客本次作业的链接 Part.2 成员汇报 组员1(组长)柯奇豪 过去两天完成了哪些任务 进一步优化代码,结合自己负责的部分修改功能 代码规范完整 ...

  7. FileInputStream和FileOutStream的使用——文件字节输入/输出流

    最近又退回到java EE的学习,这篇博客就来讲解一下字节流中最重要的两个类FileInputStream和FileOutputStream的用法: FileInputStream:全称是文件字节输入 ...

  8. Java 连接、操控数据库总结(JDBC)

    看到数据库连接不由得想起了大一末参加团队考核时的悲催经历~~,还记得当初傻傻地按照书本的代码打到 Eclipse 上,然后一运行就各种报错...报错后还傻傻地和书本的代码一遍又一遍地进行核对,发现无误 ...

  9. .Net Core 自动化部署:使用jenkins部署到linux docker容器运行

    上次我们说到.Net Core 自动化部署:使用docker版jenkins部署dotnetcore应用,这次我们使用jenkins发布我们的.NET Core站点到docker容器中运行,为后面的的 ...

  10. django response reuqest

    HttpRequest objects 属性 HttpRequest.scheme 表示请求协议的字符串(通常是http或https). HttpRequest.body 原始HTTP请求主体作为字节 ...