Skip to main content

new Thing(); is explicit that you want a constructor called whereas new Thing; is taken to imply you don't mind if the constructor isn't called.

If used on a struct/class with a user-defined constructor, there is no difference. If called on a trivial struct/class (e.g. struct Thing { int i; };) then new Thing; is like malloc(sizeof(Thing)); whereas new Thing(); is like calloc(sizeof(Thing)); - it gets zero initialized.

The gotcha lies in-between:

struct Thingy { ~Thingy(); // No-longer a trivial class virtual WaxOn(); int i; }; 

The behavior of new Thingy; vs new Thingy(); in this case changed between C++98 and C++2003. See Michael Burr's explanationMichael Burr's explanation for how and why.

new Thing(); is explicit that you want a constructor called whereas new Thing; is taken to imply you don't mind if the constructor isn't called.

If used on a struct/class with a user-defined constructor, there is no difference. If called on a trivial struct/class (e.g. struct Thing { int i; };) then new Thing; is like malloc(sizeof(Thing)); whereas new Thing(); is like calloc(sizeof(Thing)); - it gets zero initialized.

The gotcha lies in-between:

struct Thingy { ~Thingy(); // No-longer a trivial class virtual WaxOn(); int i; }; 

The behavior of new Thingy; vs new Thingy(); in this case changed between C++98 and C++2003. See Michael Burr's explanation for how and why.

new Thing(); is explicit that you want a constructor called whereas new Thing; is taken to imply you don't mind if the constructor isn't called.

If used on a struct/class with a user-defined constructor, there is no difference. If called on a trivial struct/class (e.g. struct Thing { int i; };) then new Thing; is like malloc(sizeof(Thing)); whereas new Thing(); is like calloc(sizeof(Thing)); - it gets zero initialized.

The gotcha lies in-between:

struct Thingy { ~Thingy(); // No-longer a trivial class virtual WaxOn(); int i; }; 

The behavior of new Thingy; vs new Thingy(); in this case changed between C++98 and C++2003. See Michael Burr's explanation for how and why.

Improved formating
Source Link
hivert
  • 10.7k
  • 3
  • 34
  • 58

"new Thing();"new Thing(); is explicit that you want a constructor called whereas "new Thing;"new Thing; is taken to imply you don't mind if the constructor isn't called.

If used on a struct/class with a user-defined constructor, there is no difference. If called on a trivial struct/class (e.g. struct Thing { int i; }struct Thing { int i; };) then "new Thing;"new Thing; is like "malloc(sizeof(Thing));"malloc(sizeof(Thing)); whereas "new Thing();"new Thing(); is like "calloc(sizeof(Thing));"calloc(sizeof(Thing)); - it gets zero initialized.

The gotcha lies in-between:

struct Thingy { ~Thingy(); // No-longer a trivial class virtual WaxOn(); int i; }; 

The behavior of "new Thingy;"new Thingy; vs "new Thingy();"new Thingy(); in this case changed between C++98 and C++2003. See Michael Burr's explanation for how and why.

"new Thing();" is explicit that you want a constructor called whereas "new Thing;" is taken to imply you don't mind if the constructor isn't called.

If used on a struct/class with a user-defined constructor, there is no difference. If called on a trivial struct/class (e.g. struct Thing { int i; }) then "new Thing;" is like "malloc(sizeof(Thing));" whereas "new Thing();" is like "calloc(sizeof(Thing));" - it gets zero initialized.

The gotcha lies in-between:

struct Thingy { ~Thingy(); // No-longer a trivial class virtual WaxOn(); int i; }; 

The behavior of "new Thingy;" vs "new Thingy();" in this case changed between C++98 and C++2003. See Michael Burr's explanation for how and why.

new Thing(); is explicit that you want a constructor called whereas new Thing; is taken to imply you don't mind if the constructor isn't called.

If used on a struct/class with a user-defined constructor, there is no difference. If called on a trivial struct/class (e.g. struct Thing { int i; };) then new Thing; is like malloc(sizeof(Thing)); whereas new Thing(); is like calloc(sizeof(Thing)); - it gets zero initialized.

The gotcha lies in-between:

struct Thingy { ~Thingy(); // No-longer a trivial class virtual WaxOn(); int i; }; 

The behavior of new Thingy; vs new Thingy(); in this case changed between C++98 and C++2003. See Michael Burr's explanation for how and why.

Source Link
kfsone
  • 1.1k
  • 7
  • 2

"new Thing();" is explicit that you want a constructor called whereas "new Thing;" is taken to imply you don't mind if the constructor isn't called.

If used on a struct/class with a user-defined constructor, there is no difference. If called on a trivial struct/class (e.g. struct Thing { int i; }) then "new Thing;" is like "malloc(sizeof(Thing));" whereas "new Thing();" is like "calloc(sizeof(Thing));" - it gets zero initialized.

The gotcha lies in-between:

struct Thingy { ~Thingy(); // No-longer a trivial class virtual WaxOn(); int i; }; 

The behavior of "new Thingy;" vs "new Thingy();" in this case changed between C++98 and C++2003. See Michael Burr's explanation for how and why.