auto_ptr和operator void *(); [英] auto_ptr and operator void*();

查看:62
本文介绍了auto_ptr和operator void *();的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么auto_ptr没有像这样的强制转换操作符有任何理由:


operator void *();


所以一个人可以很容易地测试auto_ptr为NULL ???

标准不会声明这样的运算符。

检查auto_ptr是否指向NULL的唯一方法是使用.get ()方法




马丁

Is there any reason why auto_ptr does not have the cast operator like this:

operator void* ();

So that one could easily test auto_ptr for NULL ???
Standard does not declare such operator.
Is the only way to check if auto_ptr points to NULL, is to use .get() method
?

Martin

推荐答案

Marcin Vorbrodt写道:
Marcin Vorbrodt wrote:
有没有什么理由说auto_ptr没有像这样的演员操作符:

operator void *();

所以那个可以很容易地测试auto_ptr是否为NULL ???
标准不会声明这样的运算符。
是检查auto_ptr是否指向NULL的唯一方法,就是使用.get()
方法?
Is there any reason why auto_ptr does not have the cast operator like
this:

operator void* ();

So that one could easily test auto_ptr for NULL ???
Standard does not declare such operator.
Is the only way to check if auto_ptr points to NULL, is to use .get()
method ?




不。还有一个,但那更加丑陋。要打电话给opertator->在其

函数表中。我不知道什么是理由忽略操作员无效* ...

它可能出现在即将出版的C ++标准书中。但是

直到那时我想最好的是在comp.lang.c ++。moderated中询问。

实际上在这个设计上工作的人大多挂在那里,因为他们放弃了这个小组的b
由于巨量的巨魔和偏离主题的帖子。 IIRC I

已经读过auto_ptr如何成为今天的样子,但我不记得

关于失踪的任何内容。操作员布尔或无效*。


根据Herb Sutter的 http://www.gotw.ca/gotw/025.htm 这是Bill

Gibbons,Greg Colvin和Steve Rumsby已经敲定了auto_ptr。


-

WW aka Attila



Nope. There is another one, but that is uglier. To call opertator-> in its
function form. I dunno what is the rationale to leave out operator void*...
It might be present in the rationale in the upcoming C++ Standard book. But
until then I guess the best is to ask in comp.lang.c++.moderated. Guys who
actually worked on this design mostly hang out there because they gave up on
this group due to the sheer amount of trolls and off-topic posts. IIRC I
have read about how auto_ptr became what it is today, but I do not recall
talling anything about the "missing" operator bool or void*.

According to Herb Sutter in http://www.gotw.ca/gotw/025.htm it was Bill
Gibbons, Greg Colvin and Steve Rumsby who has finalized auto_ptr.

--
WW aka Attila


" Marcin Vorbrodt" < MV ***** @ eos.ncsu.edu>在消息中写道

news:bk ********** @ uni00nw.unity.ncsu.edu ...
"Marcin Vorbrodt" <mv*****@eos.ncsu.edu> wrote in message
news:bk**********@uni00nw.unity.ncsu.edu...
auto_ptr是否有任何理由没有像这样的演员
运算符:

operator void *();

这样可以很容易地测试auto_ptr为NULL ???


这将允许有人写:


auto_ptr< int> p(new int);


删除p;

标准不声明此类运算符。


也不应该。 安全布尔是指安全布尔。成语会更好。

检查auto_ptr是否指向NULL的唯一方法是使用.get()方法吗?
Is there any reason why auto_ptr does not have the cast
operator like this:

operator void* ();

So that one could easily test auto_ptr for NULL ???
This would allow someone to write:

auto_ptr<int> p(new int);

delete p;
Standard does not declare such operator.
Nor should it. A "safe bool" idiom would be better.
Is the only way to check if auto_ptr points to NULL, is to
use .get() method ?




据我所知,但这不能保证不会以另一种方式获得



Dave



As far as I know, but that''s not a guarantee that there isn''t
another way.

Dave


David B. Held写道:
David B. Held wrote:
" Marcin Vorbrodt" < MV ***** @ eos.ncsu.edu>在消息中写道
新闻:bk ********** @ uni00nw.unity.ncsu.edu ...
"Marcin Vorbrodt" <mv*****@eos.ncsu.edu> wrote in message
news:bk**********@uni00nw.unity.ncsu.edu...
auto_ptr是否有任何理由没有这样的演员
运算符:

operator void *();

这样可以很容易地测试auto_ptr为NULL ???
Is there any reason why auto_ptr does not have the cast
operator like this:

operator void* ();

So that one could easily test auto_ptr for NULL ???



这将允许有人写:

auto_ptr< int> p(new int);

删除p;


This would allow someone to write:

auto_ptr<int> p(new int);

delete p;




是吗?你能删除一个空格*吗?删除操作符不会知道要使用什么析构函数(如果有的话),或者要释放的内存量

(除非它与...一起存储)以某种方式分配内存。


-Kevin

-

我的电子邮件地址有效,但会定期更改。

如需联系我,请使用最近发布的地址。



Would it? Can you delete a void *? The delete operator wouldn''t know
what destructor (if any) to use, or the amount of memory to be freed
(unless it''s stored with the allocated memory somehow).

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.


这篇关于auto_ptr和operator void *();的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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