Reputation: 2973
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
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
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