我不太清楚auto_ptr在这种情况下是否会帮助我:
class A
{
A(const B& member)
: _member(B)
{};
...
const B& _member;
};
A generateA() {
auto_ptr<B> smart(new B());
A myA(*smart);
return myA;
}
Run Code Online (Sandbox Code Playgroud)
离开其封闭范围myA._member时,引用是否有效smart?如果auto_ptr不是这里的答案,那是什么?
编辑:我看到我困惑每个人的地方; 我必须将myA返回到范围之外,这就是为什么我关心_member在智能退出范围后有效.
我看到以下代码,
#include <new>
#include <memory>
using namespace std;
class Fred; // Forward declaration
typedef auto_ptr<Fred> FredPtr;
class Fred {
public:
static FredPtr create(int i)
{
return new Fred(i); // Is there an implicit casting here? If not, how can we return
// a Fred* with return value as FredPtr?
}
private:
Fred(int i=10) : i_(i) { }
Fred(const Fred& x) : i_(x.i_) { }
int i_;
};
Run Code Online (Sandbox Code Playgroud)
请参阅功能创建中列出的问题.
谢谢
//根据评论更新
是的,代码无法传递VC8.0错误C2664:'std :: auto_ptr <_Ty> :: auto_ptr(std :: auto_ptr <_Ty>&)throw()':无法将参数1从'Fred*'转换为' std …
在我的代码中,我使用new分配一个整数数组.之后我将这个指针包装到auto_ptr.我知道auto_ptr会自动调用它的析构函数.由于我的auto_ptr指向一个数组(使用new分配),数组是否会与auto_ptr一起被删除,否则会导致内存泄漏.这是我的示例代码.
std::auto_ptr<int> pointer;
void function()
{
int *array = new int[2];
array[0] = 10;
array[1] = 20;
pointer.reset((int*) array);
}
int _tmain(int argc, _TCHAR* argv[])
{
function();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 为什么在auto_ptr中有模板复制构造函数和覆盖操作符函数?
C++的ISO标准为auto_ptr指定了以下接口.(这是直接从2003年标准中复制的.)
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> r) 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>() …Run Code Online (Sandbox Code Playgroud) 我在这里读到std :: auto_ptr <> :: operator =
但请注意,当左侧对象已指向某个对象时,它不会自动释放.您可以通过在为其分配新值之前调用成员函数reset来明确执行此操作.
但是,当我读取头文件的源代码时 C:\Program Files\Microsoft Visual Studio 8\VC\ce\include\memory
template<class _Other>
auto_ptr<_Ty>& operator=(auto_ptr<_Other>& _Right) _THROW0()
{ // assign compatible _Right (assume pointer)
reset(_Right.release());
return (*this);
}
auto_ptr<_Ty>& operator=(auto_ptr<_Ty>& _Right) _THROW0()
{ // assign compatible _Right (assume pointer)
reset(_Right.release());
return (*this);
}
auto_ptr<_Ty>& operator=(auto_ptr_ref<_Ty> _Right) _THROW0()
{ // assign compatible _Right._Ref (assume pointer)
_Ty **_Pptr = (_Ty **)_Right._Ref;
_Ty *_Ptr = *_Pptr;
*_Pptr = 0; // release old
reset(_Ptr); // set new
return (*this); …Run Code Online (Sandbox Code Playgroud) 如果我使用auto_ptr来保存指向动态分配的数组的指针,当auto_ptr被杀死时,它将使用普通删除操作而不是删除[]因此不会删除我分配的数组.
我如何(正确)在动态分配的数组上使用auto_ptr?
如果这不可能,动态分配的数组是否有另一种智能指针替代方案?
提前致谢.
我有一些第三方库生成并返回auto_ptr.但是,我真的想使用一些STL容器.
所以我猜一种方法是转换
auto_ptr <int> ptr = some_library_call ();
Run Code Online (Sandbox Code Playgroud)
进入常规的c ++指针.以下工作会吗?
int* myptr = ptr;
Run Code Online (Sandbox Code Playgroud)
如果没有,使用STL和auto_ptr的最佳方法是什么(是的,我知道它不能直接工作......我知道stl和auto_ptr不能混合在一起)?
我们能够将std :: auto_ptr转换为普通指针吗?
class Test
{
......
}
Test* function()
{
std::auto_ptr<Test> test(new Test());
return _____//TODO : need to convert this auto_ptr to Test*
}
Run Code Online (Sandbox Code Playgroud)
是否可以将本地创建的auto_ptr指针转换为普通指针.
在C++ 03环境中,您是否会使用a auto_ptr或(boost)shared_ptr从函数返回资源?(在C++ 11中,人们自然会使用unique_ptr.)
auto_ptr<T> f() {
...
return new T();
}
Run Code Online (Sandbox Code Playgroud)
要么
shared_ptr<T> f() {
...
return new T();
}
Run Code Online (Sandbox Code Playgroud)
auto_ptr有一些陷阱(至少它的构造在MSVC 2005上是非常错误的,这是我必须使用的),但shared_ptr似乎有点过分......
我不完全理解的好处unique_ptr了auto_ptr,或者我还没有完全相信,为什么我们需要使用unique_ptr.
我看到以下差异.
1)unique_ptr支持数组,因此unique_ptr析构函数调用delete []数组,而auto_ptr析构函数只调用数组delete.
2)std::move()必须使用而不是直接复制unique_ptr.但是我们有什么优势std::move()?我将尝试解释如下.
auto_ptr <int> p1 (new int);
auto_ptr <int> p2 = p1; // Now p1 is NULL and p2 exists with value of p1
unique_ptr <int> p1 (new int);
unique_ptr <int> p2 = std::move(p1); // Now p1 is NULL and p2 exists with value of p1
Run Code Online (Sandbox Code Playgroud)
那么我们将获得的优势是什么?
3)我在互联网上阅读,unique_ptr可以在容器中使用.但如果我理解正确,这不是伟大的事情unique_ptr.容器函数语义已经改变,所以现在一天,复制没有在容器函数内部完成.但这是多么伟大的事情unique_ptr呢?现在容器功能已经改变了,为什么我们不能auto_ptr …