智能指针:auto_ptr和数组 [英] Smart Pointers : auto_ptr and array
问题描述
假设我有一个auto_ptr的向量定义如下:
vector< auto_ptr< T v;
是允许的吗?
有副作用吗?
如果它'不是一个好主意,我该如何解决这个问题?
Hi,
Let''s say I have a vector of auto_ptr defined like this :
vector< auto_ptr<T v;
is it allowed ?
Is there any side effects ?
If it''s not a good idea, how can I fix this ?
推荐答案
* mosfet:
* mosfet:
>
vector< auto_ptr< T v;
允许
?
>
vector< auto_ptr<T v;
is it allowed ?
不,因为auto_ptr不可复制,好的编译器会抱怨。
No, because auto_ptr is not copyable, and a good compiler will complain.
有副作用吗?
Is there any side effects ?
是的,如果它编译。<br />
Yes, if it compiles.
如果不是好的话想法,我该如何解决这个问题?
If it''s not a good idea, how can I fix this ?
使用例如boost :: shared_ptr。
-
答:因为它弄乱了人们通常阅读文本的顺序。
问:为什么这么糟糕?
A:热门发布。
问:usenet和电子邮件中最烦人的是什么?
Use e.g. boost::shared_ptr.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
On 5/16/2007 11:41 AM,mosfet写道:
On 5/16/2007 11:41 AM, mosfet wrote:
假设我有一个auto_ptr的向量定义如下:
vector< auto_ptr< T v;
允许吗?
有副作用吗?
Hi,
Let''s say I have a vector of auto_ptr defined like this :
vector< auto_ptr<T v;
is it allowed ?
Is there any side effects ?
编号不允许。可能无法编译。不要!
No. Not allowed. Might not compile. Don''t !
如果这不是一个好主意,我该如何解决这个问题?
If it''s not a good idea, how can I fix this ?
std :: vector< boost :: shared_ptr< T v;
S.
-
Stefan Naewe
stefan dot naewe at atlas-elektronik dot com
std::vector<boost::shared_ptr<T v;
S.
--
Stefan Naewe
stefan dot naewe at atlas-elektronik dot com
5月16日,10:41,mosfet < john .... @ anonymous.orgwrote:
On 16 May, 10:41, mosfet <john....@anonymous.orgwrote:
假设我说我有一个auto_ptr的向量定义如下:
vector< auto_ptr< T v;
允许
?
Hi,
Let''s say I have a vector of auto_ptr defined like this :
vector< auto_ptr<T v;
is it allowed ?
NO。阅读 http://www.ddj.com/dept/cpp/184403719
有副作用吗?
如果这不是一个好主意,我该如何解决这个问题?
Is there any side effects ?
If it''s not a good idea, how can I fix this ?
改为使用boost / TR1 shared_ptr。
use boost/TR1 shared_ptr instead.
这篇关于智能指针:auto_ptr和数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!