C ++构造函数`new T`和`new T()`之间有什么区别?

时间:2011-01-23 08:24:46

标签: c++ new-operator

  

可能重复:
  Do the parentheses after the type name make a difference with new?

我有C / C#背景。我正在学习C ++。

在C ++中,有很多构造函数形式。我对他们感到困惑。 这些是我的理解。这是对的吗?

struct T {};

// #1
T* a = new T;  // Regular form. Just allocate memory.
// My understanding in C.
T* a = malloc(sizeof(T)); 

// #2
T* a = new T(); // #1 + Calling constructor.
// My understanding in C.
T* a = malloc(sizeof(T));
T_ctor_func(a);

0 个答案:

没有答案