2011-04-10 41 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類型來說這是不正確的。