我想返回两个值,其中一个是一个新对象.我可以这样做std::pair:
class A {
//...
};
std::pair<A*, int> getA()
{
A* a = new A;
//...
}
Run Code Online (Sandbox Code Playgroud)
为了使代码异常安全,我想做:
std::pair<std::auto_ptr<A>, int> getA()
{
std::auto_ptr<A> a(new A);
//...
}
Run Code Online (Sandbox Code Playgroud)
但是这不会编译,因为在auto_ptr不修改被复制的情况下无法auto_ptr复制.好吧,这意味着auto_ptr不像其他类型那样构成(再一次).在这种情况下返回新对象的好方法是什么?
一种选择是返回a shared_ptr而另一种是inout引用.但我正在寻找其他替代方案.我可以这样做:
class AGetter
{
void getAExecute()
{
//...
// set a_ and i_
}
std::auto_ptr<A> getA() const
{
return a_.release();
}
int getInt() const
{
return i_;
}
private:
std::auto_ptr<A> a_;
int i_;
};
Run Code Online (Sandbox Code Playgroud)
有没有更好的办法?
请考虑我最近在代码库中看到的以下示例代码:
void ClassA::ExportAnimation(auto_ptr<CAnimation> animation)
{
... does something
}
// calling method:
void classB::someMethod()
{
auto_ptr<CAnimation> animation (new CAnimation(1,2));
ClassA classAInstance;
classAInstance.ExportAnimation(animation)
... do some more stuff
}
Run Code Online (Sandbox Code Playgroud)
我不喜欢这样 - 宁愿写它:
void ClassA::ExportAnimation(CAnimation* animation)
{
... does something
}
// calling method:
void classB::someMethod()
{
auto_ptr<CAnimation> animation (new CAnimation(1,2));
ClassA classAInstance;
classAInstance.ExportAnimation(animation.get())
... do some more stuff
}
Run Code Online (Sandbox Code Playgroud)
但这确实是一个问题?
除了使用auto_ptrs的所有已知好处之外,auto_ptr是什么"最差实践"?
创建auto_ptrs的STL限制器.auto_ptrs不符合'CopyConstructable'要求.另见Scott Meyer的"Effective STL",第8项.
创建数组的auto_ptrs在销毁时,auto_ptr的析构函数使用'delete'(并且永远不会'delete []')来销毁拥有的对象,因此这段代码会产生未定义的行为:auto_ptr api(new int [42]);
不使用auto_ptr成员在类中处理copy-ctor和op =.有人可能天真地认为通过使用auto_ptr成员,不需要为类实现复制构造函数/赋值运算符.然而,即使是一个auto_ptr成员'毒化'一个类(即违反'CopyConstructable'和'Assignable'要求).在复制/分配操作期间,这些类的对象将被部分损坏.
还有更多的auto_ptr陷阱吗?
我正在阅读有关共享指针的一些注释.他们说STL与auto_ptr的第一次尝试有以下主要缺点:
我理解前两个,但我不确定最后一个意味着什么.
有人可以解释一下.
谢谢.
我可以用C++ 11中的'move'(r值引用)做些什么我不能用std::auto_ptr?(据我所知,他们是一个想法的不同实现.)
再问一个旧问题:std::auto_ptr组件是如此糟糕?
在实现工厂类时,我遇到了std::auto_ptr一些我无法理解的行为.我将问题减少到下面的小程序,所以......让我们开始吧.
考虑以下单例类:
singleton.h
#ifndef SINGLETON_H_
#define SINGLETON_H_
#include<iostream>
#include<memory>
class singleton {
public:
static singleton* get() {
std::cout << "singleton::get()" << std::endl;
if ( !ptr_.get() ) {
std::cout << &ptr_ << std::endl;
ptr_.reset( new singleton );
std::cout << "CREATED" << std::endl;
}
return ptr_.get();
}
~singleton(){
std::cout << "DELETED" << std::endl;
}
private:
singleton() {}
singleton(const singleton&){}
static std::auto_ptr< singleton > ptr_;
//static std::unique_ptr< singleton > ptr_;
};
#endif
Run Code Online (Sandbox Code Playgroud)
singleton.cpp
#include<singleton.h>o
std::auto_ptr< singleton > singleton::ptr_(0);
//std::unique_ptr< singleton > …Run Code Online (Sandbox Code Playgroud) 我的一些同事宁愿明确初始化std::auto_ptr到0在构造函数初始化列表中,但它会被初始化为0它的构造函数没有任何明确的初始化.那么有什么理由去做吗?
#include <memory>
class A
{
A() : SomePtr(0)
{
}
private:
std::auto_ptr<SomeType> SomePtr;
};
Run Code Online (Sandbox Code Playgroud) 我试图声明一个动态int数组,如下所示:
int n;
int *pInt = new int[n];
Run Code Online (Sandbox Code Playgroud)
我可以这样做std::auto_ptr吗?
我尝试过类似的东西:
std::auto_ptr<int> pInt(new int[n]);
Run Code Online (Sandbox Code Playgroud)
但它没有编译.
我想知道我是否可以使用auto_ptr构造和如何声明动态数组.谢谢!
我一直在寻找答案,但仍然无法弄清楚。
抱歉,我的工作太复杂,无法在此处复制示例代码。
我有一个函数,它获取一个指针作为参数;我使用它,但后来,我需要一种回调,我想在其中使用我的旧指向对象。
问题是,当调用此回调时,指针已被删除或释放。我的想法是在堆上创建指向对象的副本,并在回调完成时释放它。但我在指针、复制构造函数和其他东西之间迷失了方向。解决方案可能很简单,但我被困住了。
我有一个基类,它由多个派生类继承.我想创建baseClass指针的autopointer数组.当我初始化那些autopointer我得到一些编译时错误,然后我试图这样做
std::auto_ptr<base>pbase[3];
std::auto_ptr<base> b1(new derived1());
std::auto_ptr<base> b2(new derived2());
std::suto_ptr<base> b3(new derived3());
pbase[0] = b1;
pbase[1] = b2;
pbase[2] = b3;
Run Code Online (Sandbox Code Playgroud)
它工作正常,我修复了内存泄漏问题,而我一个窗口,我不使用valgrind,我使用boost框架泄漏.
编译错误:
class A{
public:
std::auto_ptr<base>pbase[2];
}
Run Code Online (Sandbox Code Playgroud)
在A.cpp文件中
A::A():pbase[0](new derived1()), pbase[1](new derived2()){
}
Run Code Online (Sandbox Code Playgroud)
我收到了错误 C2059:syntax error : '['