构造函数否认? [英] Constructor denial?

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

问题描述

是否可以构造构造函数REFUSE?也就是说,可以在构造函数中检查
,看看我是否真的想要构造,

并在构造时返回nullptr而不是有效的对象地址/>
是否尝试过?注意''在过去'',如果由于某种原因无法进行分配,则对malloc()的调用将返回

''null''。我想知道是否

这个能力是否已经保存在''gcnew'中?


我立即明白为什么这是不可能的如果你允许堆栈

语义。但我也注意到我不能在堆栈中创建数组

语义(元素可以是对象,但必须实例化数组

作为指针)。因此,这些限制,在某些情况下不允许堆栈语义,并非没有优先权......


与我的项目没有任何关系,只是一个想法来了

介意...


[== P ==]

Is it possible to have a constructor REFUSE to be constructed? That is, can
one within a constructor check to see if I really want to be constructed,
and return a nullptr instead of a valid object address when a construction
is attempted? Note that ''in the old days'', a call to malloc( ) would return
''null'' if it couldn''t make the allocation for some reason. I''m wondering if
this capability has been preserved in ''gcnew''?

Immediately I can see why this wouldn''t be possible if you allow stack
semantics. But I also have noticed that I can''t create arrays in stack
semantics (the elements can be objects, but the array must be instantiated
as a pointer). Thus, such restrictions, not allowing stack semantics for
some cases, is not without its precedence...

Not really anything to do with my projects, just a thought that came to
mind...

[==P==]

推荐答案

你可以做两件事:

- 抛出异常,但是你必须抓住它。

- 创建一个自定义的''new''运算符,如果它确定你的构造函数失败,则返回NULL。你必须建立一些机制让你的新

运算符知道构造函数失败了,但我觉得这样可行。


亲切的问候,

Bruno。

" Peter Oliphant" < PO ******* @ RoundTripInc.com>在消息中写道

新闻:Ov ************** @ tk2msftngp13.phx.gbl ...
there are 2 things you can do:
- throw an exception, but then you''ll have to catch it.
- create a custom ''new'' operator that returns NULL if it determines that
your constructor failed. you''d have to make some mechanism to let your new
operator know that the constructor failed, but that would work i think.

kind regards,
Bruno.
"Peter Oliphant" <po*******@RoundTripInc.com> wrote in message
news:Ov**************@tk2msftngp13.phx.gbl...
是否有可能有要构造的构造函数REFUSE?也就是说,构造函数中的一个可以检查我是否真的想要构造,并在尝试构建
时返回nullptr而不是有效的对象地址?请注意,在旧时代,如果无法为某些原因进行分配,则调用
malloc()将返回null。我想知道这个功能是否已经保存在''gcnew'中了?

我马上就能明白为什么如果你允许堆栈语义,这是不可能的。但我也注意到我无法在堆栈中创建数组语义(元素可以是对象,但数组必须实例化为指针)。因此,这些限制,在某些情况下不允许堆栈语义,并非没有它的优先权......

与我的项目没有任何关系,只是一个想法来到
[== P ==]
Is it possible to have a constructor REFUSE to be constructed? That is,
can one within a constructor check to see if I really want to be
constructed, and return a nullptr instead of a valid object address when a
construction is attempted? Note that ''in the old days'', a call to
malloc( ) would return ''null'' if it couldn''t make the allocation for some
reason. I''m wondering if this capability has been preserved in ''gcnew''?

Immediately I can see why this wouldn''t be possible if you allow stack
semantics. But I also have noticed that I can''t create arrays in stack
semantics (the elements can be objects, but the array must be instantiated
as a pointer). Thus, such restrictions, not allowing stack semantics for
some cases, is not without its precedence...

Not really anything to do with my projects, just a thought that came to
mind...

[==P==]



Peter Oliphant写道:
Peter Oliphant wrote:
是否可以构造一个构造函数REFUSE?
Is it possible to have a constructor REFUSE to be constructed?




是的,这就是发明异常的原因。
B. br />



Yes, this is why exceptions were invented.
B.


是的,我可以看到如何使用异常处理,但这需要

将类的所有构造尝试放在try / catch块中(在一些

级别)。


因此,这不是我解决的问题。也就是说,有一个

构造函数只返回''nullptr''拒绝构造。请注意''malloc''

返回''null''并且不需要处理*

来电者*。


现在,一种方法是将构造函数包装在另一个尝试

构造的类中,如果异常触发则返回nullptr,或指向

如果成功,则新构建的原始类实例(工厂,
如果愿意,则为b $ b)。并且这种方法可以是静态的。这样可行,但是b $ b很麻烦。


我猜它实际上不可能只返回''nullptr''

像malloc这样的建筑尝试确实......


[== P ==]


Bronek Kozicki <峰; br ** @ rubikon.pl>在消息中写道

新闻:af *************************** @ news.news-service.com。 ..
Yes, I can see how exception processing could be used, but this requires
putting all construction attempts of the class in a try/catch block (at some
level).

Therefore this isn''t a ''solution'' to what I asked about. That is, having a
constructor just return ''nullptr'' to deny construction. Note that ''malloc''
returns a ''null'' and requires no exception processing *on the part of the
caller*.

Now, one way is to wrap the constructor in another class that attempts the
construction and returns a nullptr if the exception fires, or a pointer to
the newly constructed instance of original class if successful (a ''factory'',
if you will). And such a method could be made static. That would work, but
is cumbersome.

I''m guessing it''s not possible to actually just return ''nullptr'' in a
construction attempt like ''malloc'' does...

[==P==]

"Bronek Kozicki" <br**@rubikon.pl> wrote in message
news:af***************************@news.news-service.com...
Peter Oliphant写道:
Peter Oliphant wrote:
是否可以构造一个构造函数REFUSE?
Is it possible to have a constructor REFUSE to be constructed?



是的,这是为何发明例外情况。

B。



Yes, this is why exceptions were invented.
B.



这篇关于构造函数否认?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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