复制std :: auto_ptr<>的构造函数 [英] copy constructor of std::auto_ptr<>

查看:96
本文介绍了复制std :: auto_ptr<>的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在尝试从

C ++标准库中理解std :: auto_ptr< Tclass实现作者:Nicolai Josuttis。他在4.2节给出了auto_ptr类模板的示例

实现。副本

构造函数定义为:


auto_ptr(auto_ptr& rhs)throw()

:ap(rhs。发布) ()){

}


我想知道,这里有没有泄漏?我的意思是在获取rhs所指向的内存所有权之前,不应该将内存指向

来通过ap进行关联(释放)吗?我希望复制构造函数是这样的:


auto_ptr(auto_ptr& rhs)throw(){

delete AP; //这不是必须的吗?

ap = rhs.release();

}


如果删除ap从定义中删除,我认为已经被ap指向的内存(在复制构造函数被调用之前是
)会泄漏。请评论。


提前致谢。


/ P

Hi all,

I am trying to understanding std::auto_ptr<Tclass implementation from
"The C++ standard library" by Nicolai Josuttis. He gives a sample
implementation of auto_ptr class template in section 4.2. The copy
constructor is defined as:

auto_ptr (auto_ptr& rhs) throw()
: ap (rhs. release()) {
}

I was wondering, is there not a leak here ? I mean before taking
ownership of the memory pointed to by rhs, shouldn''t the memory pointed
to by ap be relased (freed) ? I am expecting the copy constructor to be
this way:

auto_ptr(auto_ptr& rhs) throw() {
delete ap; // Isn''t this required ?
ap = rhs.release();
}

If the delete ap is removed from the definition, I think the memory
which is already being pointed to by ap (before the copy constructor is
called) will leak. Please comment.

Thanks in advance.

/P

推荐答案

dragoncoder写道:
dragoncoder wrote:

大家好,


我想了解std :: auto_ptr< Tclass实现自

" The C ++ standard library"作者:Nicolai Josuttis。他在4.2节给出了auto_ptr类模板的示例

实现。副本

构造函数定义为:


auto_ptr(auto_ptr& rhs)throw()

:ap(rhs。发布) ()){

}


我想知道,这里有没有泄漏?我的意思是在获取rhs所指向的内存所有权之前,不应该将内存指向

来通过ap进行关联(释放)吗?我希望复制构造函数是这样的:


auto_ptr(auto_ptr& rhs)throw(){

delete AP; //这不是必须的吗?

ap = rhs.release();

}


如果删除ap从定义中删除,我认为已经被ap指向的内存(在复制构造函数被调用之前是
)会泄漏。请评论。
Hi all,

I am trying to understanding std::auto_ptr<Tclass implementation from
"The C++ standard library" by Nicolai Josuttis. He gives a sample
implementation of auto_ptr class template in section 4.2. The copy
constructor is defined as:

auto_ptr (auto_ptr& rhs) throw()
: ap (rhs. release()) {
}

I was wondering, is there not a leak here ? I mean before taking
ownership of the memory pointed to by rhs, shouldn''t the memory pointed
to by ap be relased (freed) ? I am expecting the copy constructor to be
this way:

auto_ptr(auto_ptr& rhs) throw() {
delete ap; // Isn''t this required ?
ap = rhs.release();
}

If the delete ap is removed from the definition, I think the memory
which is already being pointed to by ap (before the copy constructor is
called) will leak. Please comment.



您将赋值运算符与复制构造函数混淆。根据定义,

后者仅针对尚未创建的对象调用,因此无需删除任何内容。

干杯! --M

You''re confusing the assignment operator with the copy constructor. The
latter is, by definition, only invoked for an object that has not yet
been created and therefore does not have anything to delete.

Cheers! --M




dragoncoder写道:

dragoncoder wrote:

嗨所有,


我试图理解std :: auto_ptr< Tclass实现来自

" The C ++ standard library"作者:Nicolai Josuttis。他在4.2节给出了auto_ptr类模板的示例

实现。副本

构造函数定义为:


auto_ptr(auto_ptr& rhs)throw()

:ap(rhs。发布) ()){

}


我想知道,这里有没有泄漏?我的意思是在获取rhs所指向的内存所有权之前,不应该将内存指向

来通过ap进行关联(释放)吗?我希望复制构造函数是这样的:


auto_ptr(auto_ptr& rhs)throw(){

delete AP; //这不是必须的吗?
Hi all,

I am trying to understanding std::auto_ptr<Tclass implementation from
"The C++ standard library" by Nicolai Josuttis. He gives a sample
implementation of auto_ptr class template in section 4.2. The copy
constructor is defined as:

auto_ptr (auto_ptr& rhs) throw()
: ap (rhs. release()) {
}

I was wondering, is there not a leak here ? I mean before taking
ownership of the memory pointed to by rhs, shouldn''t the memory pointed
to by ap be relased (freed) ? I am expecting the copy constructor to be
this way:

auto_ptr(auto_ptr& rhs) throw() {
delete ap; // Isn''t this required ?



不,不需要。此时没有设置ap。

复制ctors创建新对象。

它是具有对象的rhs.ap。因此...释放()...转到

转让所有权

No, its not required. ap is not set at this point.
Copy ctors create new objects.
Its the rhs.ap that has the object. therefore... release() ... to
transfer ownership


ap = rhs.release();

}


如果从定义中删除了删除ap,我认为已经被ap指向的内存

(在复制之前)构造函数是

调用)会泄漏。请评论。
ap = rhs.release();
}

If the delete ap is removed from the definition, I think the memory
which is already being pointed to by ap (before the copy constructor is
called) will leak. Please comment.



复制ctors没有被调用,它们被调用。

copy ctors aren''t called, they are invoked.


>

提前致谢。


/ P
>
Thanks in advance.

/P



这是复印件。

你很混乱的构造和赋值(auto_ptr *确实*

拥有一个对象)。


现在看一下赋值运算符(这真的是你的在

心中):


模板< typename T>

auto_ptr& operator =(auto_ptr< T>& rhs)throw()

{

reset(rhs.release()); //释放rhs.ap并(重新)设置this-> ap及其

值。

返回* this;

}


这实际上转移了所有权。


注意:


int n(5); // ctor

n = 4; // asignment

int m = n; //复制ctor - 不是作业!!!

int x(n); //复制ctor

m = x; //赋值


赋值修改现有对象,所有ctors,包括copy ctors,

创建一个* new *对象。

普通人和副本之间的区别在于成员如何获得他们的价值。

所有ctors构建(以这种或那种方式)。

作业不构造。


因为它是一个全新的物体,所以复制任何成员都是徒劳的。

对不起,我可以'别的说法。

Thats a copy ctor.
You are confusing construction and assignment (where auto_ptr *does*
own an object).

Now look at the assignment operator (which is really what you had in
mind):

template< typename T >
auto_ptr& operator= (auto_ptr< T >& rhs) throw()
{
reset(rhs.release()); // release rhs.ap and (re)set this->ap with its
value.
return *this;
}

Which in effect transfers ownership.

note:

int n(5); // ctor
n = 4; // asignment
int m = n; // copy ctor - NOT an assignment !!!
int x(n); // copy ctor
m = x; // assignment

assignments modify an existing object, all ctors, including copy ctors,
create a *new* objects.
The difference between a plain ctor and a copy ctor is how the members
are getting their values.
All ctors construct (one way or another).
Assignments do not construct.

Its futile to zap any member in a copy since its a brand new object.
Sorry, i can''t say it any other way.


* Salt_Peter:
* Salt_Peter:

>

复制ctors没有被调用,它们被调用。
>
copy ctors aren''t called, they are invoked.



C ++标准主要使用术语被叫。在一些地方,它使用了被调用这个术语来代替
。意义上没有内在的差异,但是没有b / b无论是否呼叫或被调用如果使用,有几种不同的可能含义,适用的含义取决于具体情况。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?

The C++ standard mostly uses the term "called". In a few places it uses
the term "invoked". There''s no inherent difference in meaning, but no
matter whether "call" or "invoked" is used, there are several different
possible meanings, and the one that applies depends on the context.

--
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?


这篇关于复制std :: auto_ptr&lt;&gt;的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆