1、栈对象
2、堆对象
3、全局对象 、静态全局对象
4、静态局部对象
#include <iostream>
using namespace std;
class Test{
public:
Test(int n):n_(n){
cout<<"Test"<<n_<<endl;
}
~Test(){
cout<<"~Test"<<n_<<endl;
}
private:
int n_;
};
int m2; //未经初始化得全局变量 该变量存储与.bss段中,(block started by symbol)
int m3=300;//已经初始化全局变量 该变量存储与.data段中。
Test test4(100);//全局对象得构造先于main函数
static Test test5(200);
int main(){
cout<<"Enter main...."<<endl;
cout<<m2<<endl;
cout<<m3<<endl;
Test test(10);//栈上创建得对象将自动释放对象
{
Test test2(20);
}
{
Test * test3=new Test(30);//堆上创建得对象,要显示释放
delete test3;
}
static int m4; //该变量存储与.bss段中,(block started by symbol)编译器初始化
static int m5=500; // 该变量存储与.data段中。
static Test test4(600); //运行期初始化 该对象存储与.data段中。
cout<<"Exit main...."<<endl;
return 0;
}