Viku
Viku

Reputation: 2973

confused with the object creation

I was just reading one article and came through the below code

int var  = int();

can anyone please let me know what is the importance of following the above concept instead of going for creating an object using new operator or general stack object .

Upvotes: 0

Views: 66

Answers (2)

Umpong_1947676
Umpong_1947676

Reputation: 111

This line of code avoids getting warnings like "Value not initiated" or getting default NULL values. It's like giving you an assurance that you are not working on a vaiable that does not exist at all.

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258568

That syntax also value-initializes the variable. It's equivalent to writing int var = 0;.

A simple int var; wouldn't do that (in most contexts) - it would leave the variable uninitialized.

new would dynamically allocate the object and should only be used when really necessary.

Upvotes: 3

Related Questions