为什么`throw MyClass'不起作用而且`throw MyClass()'呢?(Why `throw MyClass' doesn't work and

编程入门 行业动态 更新时间:2024-10-25 11:24:46
为什么`throw MyClass'不起作用而且`throw MyClass()'呢?(Why `throw MyClass' doesn't work and `throw MyClass()' does?)

我在某处读到(现在找不到来源)

MyClass *p1 = new MyClass;

MyClass *p2 = new MyClass();

只要MyClass提供默认构造函数,它们本质上是等价的。 编译器理解我想要做的事情并添加空括号。

如果是这种情况,为什么我不被允许写

throw MyException;

但必须使用

throw MyException();

? (是的,一行开头的问号。)

为了增加一些混淆, C ++ FAQ建议第二个用例( new MyClass() )不调用构造函数,而是调用用operator()定义的函数。

I read somewhere (cannot find the source now) that

MyClass *p1 = new MyClass;

and

MyClass *p2 = new MyClass();

are essentially equivalent, provided that MyClass provides a default constructor. The compiler understands what I want to do and adds the empty parentheses.

If this is the case, why I am not allowed to write

throw MyException;

but have to use

throw MyException();

? (Yep, a question mark at the beginning of a line.)

To add some more confusion, the C++ FAQ suggests that the second usecase (new MyClass()) does not invoke a constructor, but calls function defined with operator() instead.

最满意答案

编译器理解我想要做的事情并添加空括号。

不,不; 这两个表达式并不完全相同。 区别在于如何初始化对象:第一个使用默认初始化,而第二个使用值初始化。 因此它们对于定义默认构造函数的类是等效的; 否则,第一个将使POD对象保持未初始化,而第二个将初始化为零。

为什么我不允许写throw MyException; ?

MyException()是一个创建值初始化临时对象的表达式; 你可以抛出它就像你可以抛出任何其他合适的表达式的值。

MyException不是表达式; 它只是一个类型名称。 你只能抛出一个表达式的值,所以throw MyException; 无效。 无法创建默认初始化临时。

为了增加一些混淆,C ++ FAQ建议第二个用例(new MyClass())不调用构造函数,而是调用用operator()定义的函数。

不,不。 它说像List x();这样的声明List x(); 声明一个返回类型为List的函数,而不是(如人们所想)的List类型的初始化对象。 它与new-expressions或operator()无关。

The compiler understands what I want to do and adds the empty parentheses.

No it doesn't; the two expressions aren't quite equivalent. The difference is in how the objects are are initialised: the first uses default-initialisation, while the second uses value-initialisation. So they are equivalent for classes that define a default constructor; otherwise, the first will leave POD objects uninitialised, while the second will initialise them to zero.

why I am not allowed to write throw MyException;?

MyException() is an expression that creates a value-initialised temporary object; you can throw that just like you can throw the value of any other suitable expression.

MyException isn't an expression; it's just a type name. You can only throw the value of an expression, so throw MyException; is not valid. There's no way to create a default-initialised temporary.

To add some more confusion, the C++ FAQ suggests that the second usecase (new MyClass()) does not invoke a constructor, but calls function defined with operator() instead.

No it doesn't. It says that a declaration like List x(); declares a function with a return type List, not (as one might think) a value-initialised object of type List. It has nothing to do with new-expressions or operator().

更多推荐

本文发布于:2023-07-24 00:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239186.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   throw   MyClass   work

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!