一步一步写STL:定制boost::shared_ptr

       某知名大师说过:自从有了智能指针,上一次发生内存泄露还是在2004年当实习生的时候!

       由此可见智能指针威力无穷,竟让他8年没发生过内存泄露,我们上一篇给出了auto_ptr的实现方案,今天给出shared_ptr和实现版本,在新标准中auto_ptr已经不复存在了(其实存在不存在也没什么关系,你想用的话随时可以自己写一个啊)

取而代之的是引入了boost的shared_ptr智能指针和其他三个,这是一个引用计数型的类指针,相比auto_ptr。他有一个至关重要的优点——可以当成类型放入容器,这点auto_ptr是办不到的!

 

根据Effective C++上对shared_ptr的描述,我们很容易想到他的内部实现,以下是我写的实现版本:

template<class _Ty>
class shared_ptr
{
	typedef _Ty  element_type;
	typedef _Ty* pointer;
private:
	_Ty* _ptr;
	static std::size_t n_count;
	static void change_count_num(bool )throw();

public:

	explicit shared_ptr(pointer p=0 )throw()
		:_ptr(p)
	{//构造函数 只能被显示初始化
		shared_ptr::change_count_num(true);
		 //std::cout<<shared_ptr::n_count<<std::endl;
	}

	shared_ptr(const shared_ptr &sPtr)throw()
		:_ptr( sPtr.get() )
	{//复制构造函数
		shared_ptr::change_count_num(true);
		 //std::cout<<shared_ptr::n_count<<std::endl;
	}

	shared_ptr& operator= (const shared_ptr &sPtr)throw()
	{//赋值操作符
		_ptr = sPtr.get();
		shared_ptr::change_count_num(true);
		 //std::cout<<shared_ptr::n_count<<std::endl;
		return *this;
	}

	template<typename other>
	shared_ptr(const shared_ptr<other> &sPtr)throw()
		:_ptr( sPtr.get() )
	{//重载模版复制函数,用于实现继承体系中的多态性
		shared_ptr::change_count_num(true);
	}

	template<typename other>
	shared_ptr& operator= (const shared_ptr<other> &sPtr)throw()
	{//赋值操作符
		_ptr = sPtr.get();
		shared_ptr::change_count_num(true);
		return *this;
	}

	~shared_ptr()throw()
	{//析构函数,判断计数是否为0
		if(shared_ptr::n_count == 0)
		{
			delete _ptr;
			_ptr = 0;
		}
		 
	shared_ptr::change_count_num(false);	}

	pointer get()const throw()
	{//get器,RAII的设计原则,参见Effective C++
		return _ptr;
	}

	void release()throw()
	{//释放掉对其的拥有,并置于0
		_ptr = 0;
		shared_ptr::change_count_num(false);
	}

	pointer operator-> ()throw(){
		return _ptr;
	}
	element_type operator* (){
		return *_ptr;
	}
	operator bool()const throw(){
		return _ptr;
	}

};

template<typename _Ty>
 std::size_t shared_ptr<_Ty>::n_count = -1;

 template<typename _Ty>
 void shared_ptr<_Ty>::change_count_num (bool flag)
 {
	 flag? ++shared_ptr<_Ty>::n_count : --shared_ptr<_Ty>::n_count;
 }

其中对于引用计数的实现,我使用的static成员,用于标识该类的计数,值得说明的主要就是在所有构造函数和赋值函数 使用静态函数change_count_num,传递true表示加1,false表示减1,并在析构函数执行相应的操作,相对较简单,已基本测试!其中注释的地方为测试计数使用的输出语句!

 

 

 

 

 


 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
错误提示 E0135 表示 boost::shared_ptr<pcl::visualization::PCLVisualizer> 类没有成员函数 addSphere。这可能是因为使用的 PCL 库版本较旧,或者在代码中存在错误。 在较新的 PCL 版本中,addSphere 函数已经被移除,因此无法直接使用 boost::shared_ptr<pcl::visualization::PCLVisualizer> 类的对象来添加球体。相反,可以使用 addPointCloud 函数来添加点云,并通过设置点云渲染属性来添加球体的表示。 下面是一个示例代码,展示如何使用 addPointCloud 和 setPointCloudRenderingProperties 函数来添加球体表示: ```cpp pcl::visualization::PCLVisualizer viewer("Point Cloud Viewer"); // 创建一个空的点云对象 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); // 填充点云数据到cloud对象中 // 创建一个点云索引对象,用于存储圆柱体的点索引 pcl::PointIndices::Ptr cylinder_inliers(new pcl::PointIndices); // 填充圆柱体的点索引到cylinder_inliers对象中 // 将点云数据可视化 pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> color_handler(cloud, 255, 0, 0); // 设置颜色为红色 viewer.addPointCloud<pcl::PointXYZ>(cloud, color_handler, "cloud"); viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "cloud"); // 高亮显示圆柱体的点索引 for (int i = 0; i < cylinder_inliers->indices.size(); ++i) { int index = cylinder_inliers->indices[i]; pcl::PointXYZ point = cloud->points[index]; viewer.addSphere(point, 0.01, 0, 255, 0, "cylinder_inliers_" + std::to_string(i)); } // 显示点云及圆柱体点索引 while (!viewer.wasStopped()) { viewer.spinOnce(); } ``` 请注意,上述代码中的 viewer 对象是 pcl::visualization::PCLVisualizer 类型的对象,而不是 boost::shared_ptr 智能指针。此外,使用的添加球体函数是 viewer.addSphere 而不是 boost::shared_ptr<pcl::visualization::PCLVisualizer> 对象的方法。 如果你的 PCL 版本较旧,可能无法直接使用上述方法。在这种情况下,建议升级到较新的 PCL 版本,以获得最新的功能和修复的 bug。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值