这本书Object oriented programming in c++ by Robert Lafore说,
静态局部变量具有自动本地的可见性 变量(即包含它的函数内部)。但是,它 生命周期与全局变量的生命周期相同,除了它 直到第一次调用函数时才会出现 包含它。此后它仍然存在于生命中 程序
首次调用函数后会出现什么意思?静态本地存储在程序加载到内存时分配。
答案 0 :(得分:3)
在输入.menu-icon {
width:30px;
height:30px;
background-image:url('https://cdn2.iconfinder.com/data/icons/ui-icon-part-2/128/menu_hamburger-128.png');
background-size:30px 30px;
display:inline-block;
position:relative;
left:10px;
top:5px;
cursor:pointer;
}
#holder {
border-right:10px maroon solid;
width: 200px;
height: 100%;
background-color:#999;
position:relative;
left:-170px;
-webkit-transition: left 1s;
transition: left 1s;
z-index:10;
box-shadow:1px 3px 5px rgba(0, 0, 0, 0.7);
}
#holder:hover {
left:0;
-webkit-transition: left 1s;
transition: left 1s
}
#holder h1 {
margin-left:10px;
}
#categories {
font-weight: 800;
top: .25%;
left: 2%;
}
#holder .select {
width:100%;
margin-right:5px;
font-size: 175%;
border-bottom:dotted #888 1px;
}
#holder .select a {
color:#222;
left: 3%;
text-align:center;
display:block;
padding:3px 0;
text-decoration:none;
}
#holder .select a:hover {
color:#E0E0E0;
background-color:maroon;
}
.data-div {
width:calc(100% - 80px);
height:auto;
padding:5px;
position:absolute;
top:22px; /* enter this manually because it depends on the #holder menu height */
left:-100%;
transition: left 1s ease-in;
background-color:#EEE;
}
.data-div h2{text-align:center;}
.slide-in{
left:50px;
-webkit-transition:left .7s ease-in;
transition:left .7s ease-in;
}
.slide-out{
left:-100%;
-webkit-transition:left .5s ease-out;
transition:left .5s ease-out;
}
之前分配存储,但是(例如)如果静态对象具有副作用的ctor,则这些副作用可能会延迟到第一次调用该函数之前。< / p>
但请注意,这不是必然的情况。只有在输入该块之前才需要进行常量初始化(不一定只是执行“跨越”该定义)。同样,允许实现在某些情况下早于初始化其他块作用域静态变量(如果您想了解情况的详细信息,可以查看[basic.start.init]和[stmt.dcl] ,但它基本上归结为:只要它不影响它初始化的值。例如,如果你有类似的东西:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="holder">
<h1 id="categories">Categories<i class="menu-icon"></i></h1>
<div class="select"> <a class="catselect" href="#comedy">Comedy</a>
</div>
<div class="select"> <a class="catselect" href="#horror">Horror</a>
</div>
<div class="select"> <a class="catselect" href="#action">Action</a>
</div>
</div>
<div id="data-container"></div>
<div id="comedy" class="data-div"><h2>Comedy Data</h2><p>Caffeine irish breve kopi-luwak grounds at pumpkin spice. Cinnamon arabica caffeine a mazagran siphon aroma acerbic. Sit french press seasonal id decaffeinated so trifecta. And doppio decaffeinated, galão cup grounds robusta irish black steamed.
</p><p>
Organic, rich, cup galão black caffeine filter. Aroma, whipped, so con panna organic java mocha. Turkish, cinnamon plunger pot rich pumpkin spice frappuccino caramelization. Spoon bar caramelization coffee iced, robust cup doppio id mug galão.
</p><p>
In acerbic, wings mocha robust qui sugar so as at beans ristretto. Flavour et, est sit frappuccino plunger pot doppio. Body, in saucer plunger pot macchiato lungo frappuccino irish shop. Fair trade, affogato irish coffee to go extraction single origin at dark blue mountain acerbic.</p></div>
<div id="horror" class="data-div"><h2>Horror Data</h2><p>Cortado con panna, as strong, et, grinder milk, caramelization instant flavour galão single shot. Barista bar, strong caramelization cappuccino java robusta foam kopi-luwak. Cream in seasonal, cappuccino, siphon sugar shop qui id crema. Milk, cup sit coffee grounds affogato sweet acerbic.
</p><p>
Mazagran, carajillo grounds java, acerbic, qui aftertaste brewed espresso crema. So frappuccino variety, turkish, beans, mug blue mountain galão strong robusta irish affogato. Skinny irish, french press caramelization qui barista cream. Filter, extra ut cup mug steamed cream that foam.
</p><p>
Mocha, extraction, coffee cream seasonal a shop sweet foam. Carajillo, sit carajillo redeye in extraction, black robust turkish redeye grounds. Cinnamon cream skinny id mocha half and half cappuccino trifecta milk french press arabica. Cinnamon trifecta spoon espresso siphon plunger pot black.
</p><p>
Arabica seasonal medium froth trifecta grinder single origin. And, at, eu, single shot froth cortado, plunger pot so et sugar medium. Sweet blue mountain eu java rich eu instant fair trade redeye. Coffee café au lait single shot, filter acerbic doppio roast dripper turkish shop ristretto skinny.
</p><p>
Lungo grounds, decaffeinated barista brewed bar and robust crema lungo. Mug arabica, viennese café au lait turkish roast sit est plunger pot. Lungo, java decaffeinated sit wings robust cream latte sugar crema strong. Java con panna, mug iced extra dripper single shot kopi-luwak organic.
</p><p>
Milk seasonal frappuccino variety, galão americano, white spoon beans irish coffee. Steamed single origin half and half, blue mountain mug carajillo foam lungo est macchiato. Filter, single shot siphon cup frappuccino lungo grounds. Plunger pot, at americano skinny bar brewed redeye.</p></div>
<div id="action" class="data-div"><h2>Action Data</h2><p>That, skinny, black, strong seasonal ristretto aged lungo. Black, eu robust single origin to go black froth blue mountain cinnamon wings. Dripper fair trade dark barista foam arabica strong. Skinny organic, french press americano, body con panna siphon aroma flavour.
</p><p>
Breve redeye, dark, sugar strong mug doppio plunger pot breve. Shop, turkish strong variety cinnamon mocha at chicory redeye cream strong aroma. Wings macchiato, at coffee acerbic galão seasonal cinnamon variety. Cup caffeine et at brewed con panna mocha et dark.</p></div>
...在输入块之前,实现将无法初始化main
,因为初始化它的值在它们之前是不可知的。另一方面,如果你有:
int i;
std::cin >> i;
{
static int x = i;
...实现可以在它希望的时候进行初始化(并且大多数会/将在编译时基本上执行这样的初始化,因此它不会涉及执行任何指令在运行时完全)。然而,即使对于不太重要的情况,在逻辑上可能的情况下允许早期初始化(例如,该值不是来自先前的执行)。
答案 1 :(得分:2)
在对象的C ++ 存储持续时间中(当为其分配原始内存时)和对象的生存期是两个独立的概念。提交人在讨论对象的存在时,显然指的是后者#34;
一般情况下,为一个对象分配存储空间并不足以使其成为现实&#34;。一旦初始化完成,非平凡初始化的对象的生命周期就开始了。例如,具有非平凡构造函数的类的对象不能正式生成&#34; live&#34;直到它的构造函数完成执行。
当控件第一次传递声明时,将执行静态本地对象的初始化。在此之前,对象不会正式存在,即使已经分配了它的内存。
请注意,作者在描述中并不精确。仅调用包含声明的函数是不够的。控件必须通过对象的声明才能开始其生命周期。如果函数包含分支,则在第一次调用函数时不一定会发生这种情况。
对于具有简单初始化的对象(如int
个对象),存储持续时间和生存期之间没有区别。对于这样的对象,需要分配内存。但一般情况下单独分配内存是不够的。
答案 2 :(得分:1)
这意味着函数内的静态变量在第一次调用该函数之前不会被构造函数或赋值运算符初始化。
答案 3 :(得分:1)
一旦调用包含静态局部变量的函数,就会初始化静态局部变量。