四种对象的作用域

 

 

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值