本文介绍了如何在C ++ 11中使用带有引用类型实例对象的矢量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 有可能在C ++ 11中有std :: vector没有可复制对象(有引用实例)?Is it possible to have std::vector with no copyable object (has reference instance) in C++11 ? c 在 CanNotCopy 实例中引用,存在悬挂引用的风险。resize() is unavailable with std::reference_wrapper<CanNotCopy> because it is not default constructible. However, this solution is fragile as there are lifetime dependencies on the CanNotCopy referenced and the int referenced within the CanNotCopy instance, running a risk of dangling references.解决方案是使用 std :: unique_ptr< CanNotCopy> 作为元素类型它是可移动的和默认构造的):A solution is to use std::unique_ptr<CanNotCopy> as the element type (which is moveable and default constructible):std::vector<std::unique_ptr<CanNotCopy>> hoge;hoge.resize(5);int j = 19;hoge[1].reset(new CanNotCopy(j));std::cout << hoge[1]->intref_ << "\n";对 int CanNotCopy 仍然存在。 这篇关于如何在C ++ 11中使用带有引用类型实例对象的矢量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-20 06:45