我想做这样的事情:
typedef X* X_Pointer;
boost::ptr_vector<X_Pointer> myvec;
X_Pointer x = new X();
myvec.push_back(x);
我希望我的对象都被指针引用,这样它们的复制构造函数就不会被调用,而且我还希望ptr_vector
在整个向量超出范围时控制内存管理。
但是,编译器抱怨最后一行。我认为这是因为我正在存储X*
,而不仅仅是X
.
X 只包含原始类型,以防有人询问。
如何使用ptr_vector
to 存储X*
?
编辑:
error : no instance of overloaded function "boost::ptr_vector<T, CloneAllocator, Allocator>::push_back [with T=X_Ptr, CloneAllocator=boost::heap_clone_allocator, Allocator=std::allocator<void *>]" matches the argument list
argument types are: (X_Ptr)
object type is: boost::ptr_vector<X_Ptr, boost::heap_clone_allocator, std::allocator<void *>>
myvec.push_back(x);
^