itk库的智能指针该怎么用啊?

JinCraft 2012-10-20 09:01:58
itk类库函数GetOutput都直接返回指针OutputImage*
为什么我自己编写一个函数直接返回指针就崩溃,必须返回一个智能指针itk::OutputImage::Pointer?
智能指针到底该怎么用啊?
为什么库函数里有很多时候直接用指针的?
什么时候直接用指针,什么时候用智能指针?
...全文
271 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
JinCraft 2012-10-21
  • 打赏
  • 举报
回复
楼上的,ITK不是用的STL的智能指针好不。
Q3277631 2012-10-21
  • 打赏
  • 举报
回复
namespace std {

template <class Y> struct auto_ptr_ref {};


template <class X>
class auto_ptr {
public:
typedef X element_type;

// 20.4.5.1 construct/copy/destroy:
explicit auto_ptr(X* p =0) throw();
auto_ptr(auto_ptr&) throw();
template <class Y> auto_ptr(auto_ptr<Y>&) throw();

auto_ptr& operator=(auto_ptr&) throw();
template <class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
auto_ptr& operator=(auto_ptr_ref<X>) throw();

~auto_ptr() throw();

// 20.4.5.2 members:
X& operator*() const throw();
X* operator->() const throw();
X* get() const throw();
X* release() throw();
void reset(X* p =0) throw();

// 20.4.5.3 conversions:
auto_ptr(auto_ptr_ref<X>) throw();
template <class Y> operator auto_ptr_ref<Y>() throw();
template <class Y> operator auto_ptr<Y>() throw();
};

}
[编辑] unique_ptrC++11 中提供了​std::unique_ptr​, 定义在 ​<memory>​头文件中.

C++11 新增了move语义, 相比copy语义, 它能更好的实现值传递.​std::auto_ptr​使用的是copy语义,为了向前兼容,C++11 没有修改std::auto_ptr,而是引入了新的使用move语义的​std::unique_ptr​.

uniqu_ptr的拷贝构造函数和赋值运算符都声明为deleted, 也就是说它不能被拷贝,只能通过​std::move​来转递它所指向的内存的所有权.

std::unique_ptr<int> p1(new int(5));
std::unique_ptr<int> p2 = p1; // 编译会出错
std::unique_ptr<int> p3 = std::move(p1); // 转移所有权, 现在那块内存归p3所有, p1成为无效的指针.

p3.reset(); //释放内存.
p1.reset(); //实际上什么都没做.
​std::auto_ptr​ 依然存在, 但在C++11中被标为"弃用".

[编辑] shared_ptr 和 weak_ptr基于Boost库, C++11 加入了​shared_ptr​和​weak_ptr​. 它们最早在TR1中就被引入, 但在C++11中, 在Boost的基础上又加入了新的功能.

​std::shared_ptr​使用引用计数. 每一个​shared_ptr​的拷贝都指向相同的内存. 在最后一个​shared_ptr​析构的时候, 内存才会被释放.

std::shared_ptr<int> p1(new int(5));
std::shared_ptr<int> p2 = p1; // 都指向同一内存.

p1.reset(); // 因为p2还在,所以内存没有释放.
p2.reset(); // 释放内存, 因为没有shared_ptr指向那块内存了.
​std::shared_ptr​ 使用引用计数, 所以有循环计数的问题. 为了打破循环,可以使用​std::weak_ptr​. 顾名思义, weak_ptr是一个弱引用, 只引用, 不计数. 如果一块内存被shared_ptr和weak_ptr同时引用, 当所有shared_ptr析构了之后,不管还有没有weak_ptr引用该内存, 内存也会被释放. 所以​weak_ptr​不保证它指向的内存一定是有效的, 在使用之前需要检查.

std::shared_ptr<int> p1(new int(5));
std::weak_ptr<int> wp1 = p1; // 还是只有p1有所有权.

{
std::shared_ptr<int> p2 = wp1.lock(); // p1和p2都有所有权
if(p2) // 使用前需要检查
{
// 使用p2
}
} // p2析构了, 现在只有p1有所有权.

p1.reset(); // 内存被释放.

std::shared_ptr<int> p3 = wp1.lock(); // 因为内存已经被释放了, 所以得到的是空指针.
if(p3)
{
// 不会执行到这.
}

24,857

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧