我正在尝试使用C++x0中的unique_ptr,方法如下:
#include <memory>
使用 -std=c++0x 编译时,出现了许多错误,以下是其中的一个例子。
/usr/lib/gcc/x86_64-redhat-linux/4.4.4/../../../../include/c++/4.4.4/bits/unique_ptr.h:214: error: deleted function ‘std::unique_ptr<_Tp, _Tp_Deleter>::unique_ptr(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = Descriptor, _Tp_Deleter = std::default_delete<Descriptor>]’
更新**** 这是我正在尝试的事情,我已经删除了typedefs,以便您可以清晰地看到类型。
static std::unique_ptr<SomeType> GetSomeType()
{
std::unique_ptr<SomeType> st("Data","Data2",false);
std::unique_ptr<OtherType> ot("uniportantconstructor data");
st->Add(ot);
return st;
}
//Public method of SomeType
void Add(std::unique_ptr<OtherType> ot)
{
//otmap is std::map<std::string,std::unique_ptr<OtherType> >
//mappair is std::Pair<std::string,std::unique_ptr<OtherType> >
otMap.Insert(mappair(ot->name(),ot));
}
更新:
如果我的类SomeType有一个方法,它可以返回Map中的元素(使用key),如下所示:
std::unique_ptr<OtherType> get_othertype(std::string name)
{
return otMap.find(name);
}
那么这将确保调用者收到的是映射中的指针,而不是副本?
unique_ptr
没有Add
方法。你是否意思是通过st->Add
方法实现? - fredoverflow