Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not using parentheses in constructor call with new (c++) [duplicate]

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

So I had in my main:

Class* pC = new Class;

It was working as

Class* pC = new Class();

I realized just today that I had omitted the parentheses (so I was hit by the "opposite" of the most vexing parse in a way).

My question: Are these two forms equivalent ?

like image 215
Mr_and_Mrs_D Avatar asked Mar 06 '11 14:03

Mr_and_Mrs_D


1 Answers

If the class has a default constructor defined, then both are equivalent; the object will be created by calling that constructor.

If the class only has an implicit default constructor, then there is a difference. The first will leave any members of POD type uninitialised; the second will value-initialise them (i.e. set them to zero).

like image 102
Mike Seymour Avatar answered Oct 23 '22 09:10

Mike Seymour