在C++中,以下代码如何工作?

2 c++

可能重复:
类型名称后的括号是否与new有所不同?

以下代码执行哪些操作:

Test *t1 = new Test; // there is no () after new Test;
Run Code Online (Sandbox Code Playgroud)

如果有用户声明默认构造函数

那这个呢:

Test *t2 = new Test(); // there is () after new Test;
Run Code Online (Sandbox Code Playgroud)

Cat*_*lus 7

它们是等价的,在这两种情况下都会调用没有参数的ctor.

  • 错误,请参阅http://stackoverflow.com/questions/620137/do-the-parentheses-after-the-type-name-make-a-difference-with-new. (3认同)
  • @ybungalobill:问题是关于提供ctor的类型. (3认同)
  • 不太相同...... (2认同)