2

Possible Duplicate:
Do the parentheses after the type name make a difference with new?

In some code, I recently saw a struct like this:

typedef struct MyStruct {
    int numberOne;
    int numberTwo;
} MYSTRUCT;

Later, I tried instantiating one of these structs using

MyStruct *pStruct = new MyStruct();

which worked fine with Visual Studio 2010, but failed with an obscure linker error on a different compiler. It took a while until we found out that omitting the braces like this

MyStruct *pStruct = new MyStruct;

solved the issue.

So, what exactly is the difference between these two invocations and which one is the right one to use?

Community
  • 1
  • 1
bastibe
  • 16,551
  • 28
  • 95
  • 126

1 Answers1

1

new MyStruct performs default initialization, which in your case does nothing.

new MyStruct() performs value initialization, which in your case sets both int variables to zero.

fredoverflow
  • 256,549
  • 94
  • 388
  • 662