C++编程思想 第1卷 第6章 初始化和清除 清除定义块 内存分配

一个变量可以在任何地方定义
但在定义之前是无法分配内存的
在变量定义前无法访问存储空间
 

//: C06:Nojump.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
// Can't jump past constructors

class X {
public:
  X();
};

X::X() {}

void f(int i) {
  if(i < 10) {
   //! goto jump1; // Error: goto bypasses init
  }
  X x1;  // Constructor called here
 jump1:
  switch(i) {
    case 1 :
      X x2;  // Constructor called here
      break;
  //! case 2 : // Error: case bypasses init
      X x3;  // Constructor called here
      break;
  }
} 

int main() {
  f(9);
  f(11);
}///:~

goto和switch都可能跳过构造函数调用的点
构造函数没有调用时,后面的程序块起作用

内存的分配是在堆栈中进行的。
内存分配通过向下移动堆栈指针实现的

无输出

猜你喜欢

转载自blog.csdn.net/eyetired/article/details/81140753