Not using parentheses in constructor call with new

2020-02-03 10:52发布

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 ?

1条回答
老娘就宠你
2楼-- · 2020-02-03 11:18

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).

查看更多
登录 后发表回答