运行结果:
wuzesheng@wuzesheng-ubuntu:~/Program$ ./a.out
operator new called
constructor called
#######
operator new called, line: 68, func: main
constructor called
#######
operator new called
constructor called
constructor called
constructor called
constructor called
constructor called
constructor called
constructor called
constructor called
constructor called
constructor called
#######
~A()
delete2: 8
#######
~B()
~A()
delete2: 16
#######
~A()
~A()
~A()
~A()
~A()
~A()
~A()
~A()
~A()
~A()
delete1
#######
delete1
  上面的程序很简单,我在这里不做过多的解释,感兴趣的朋友可以自己分析一下。
  通过我上面的讲解,相信大多数朋友应该对C++中内存的动态分配与释放有了较为深入的理解。后续我还有可能写一些关于C++中内存管理的文章,只有把本文所讲的内容与后续的内存管理的一些常见的方法结合起来,我们才写出更加健壮的C++程序。欢迎读者朋友留言一起交流!