#include <boost/shared_array.hpp>

 

共享数组

共享数组的行为类型于共享指针。关键不同在于共享数组在析构时,默认使用delete[]操作符来释放所含的对象。因为这个操作符只能用于数组对象,共享数组必须通过动态分配的数组的地址来初始化。共享数组对应的类型是boost::shared_array,它的定义在boost/shared_array.hpp里。

 

 1 #include <iostream>
 2 #include <boost/shared_array.hpp>
 3 
 4 class runclass
 5 {
 6 public:
 7     int i = 0;
 8 public:
 9     runclass(int num) :i(num)
10     {
11         std::cout << "i creator " << i << std::endl;
12     }
13     runclass()
14     {
15         std::cout << "i creator " << i << std::endl;
16     }
17     ~runclass()
18     {
19         std::cout << "i delete " << i << std::endl;
20     }
21     void print()
22     {
23         std::cout << "i=" << i << std::endl;
24     }
25 };
26 
27 void testfunarray()
28 {
29     boost::shared_array<runclass>p1(new runclass[5]);
30     boost::shared_array<runclass>p2(p1);//浅拷贝,内存共享,没有调用构造函数
31 }
32 
33 void main()
34 {
35     testfunarray();
36 }

 

转载于:https://www.cnblogs.com/denggelin/p/5768745.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值