2011-04-10 81 views
2

可能重复:
Do the parentheses after the type name make a difference with new?这两个C++语句有什么区别?

在C++中,就是这两个语句之间的区别?

Class clg 
{ 

    public : 

    int x,y,z; 

}; 

int main(void) 

{ 

    clg *ptrA = new clg; // 

    clg *ptrB = new clg(); // what is the importance of "()" ??? 

return 0; 

} 
+1

重复的[类型名称后的圆括号与新的有所不同?](http://stackoverflow.com/questions/620137/do-the-parentheses-after-the-type-name-make-a -difference-with-new) – 2011-04-10 08:19:30

+0

可能的重复项:[类型名称后面的圆括号是否与新的有所不同?](http://stackoverflow.com/questions/620137/do-the-parentheses-after-the-type名称使一个新的差异),[不使用括号在新的(c + +)构造函数调用](http://stackoverflow.com/questions/5211090/not-using-parentheses-in-constructor- call-with-new-c),[没有参数的语言标准的C++构造函数没有括号?](http://stackoverflow.com/questions/2318650/is-no-parentheses-on-ac-constructor-用-no-arguments-a-language-standard) – 2011-04-10 08:20:30

+0

谢谢James和Cody。 – Uday 2011-04-10 08:26:05

回答

-1

实际上?没什么,一堂课。

在引擎盖下,一个调用该类的显式构造函数,而另一个调用默认的构造函数。在任何情况下,你的构造函数都会做同样的事情(在上面的例子中,虽然理论上你可以调用复制构造函数),但是对于POD类型来说这是不正确的。