Bloomberg LP Interview Question Software Engineer / Developers
0of 0 voteswhat is the difference between
T *o = new T and T *o = new T()
If T is a user defined class/struct, there is no difference.
However,
int *o = new int; // *o has not been initialized
int *o = new int(); // *o has been default initialized, ie *o == 0
Hey I tested that code too.. result is different..
int *w=new int; prints garbage and the other one prints zero
both have the same functionality...in first case the constructor is called implicitly while in the second case constructor is explicitly called
If this is the case then below code should not compile:
class test
{
public:
explicit test() {
cout<<"constructor called"<<endl;
}
};
void main()
{
test *ptr1 = new test;
test *ptr2 = new test();
getchar();
}

If there is a default constructor then both are same, otherwise different.
- maX on June 15, 2011 Edit | Flag ReplyAccording to the latest C++ 2003 standard,
new T calls default initializer and,
new T() calls value initializer.
No constructor :
new T:
It causes default initialization of non-POD(*1) types.
POD types remain uninitialized. That is, indeterminate value.
new T():
For non-POD types default initialization.
POD types are zero initialized.
If there is a constructor which doesn't list the variables, then the same thing happens, default initialization for non-POD and zero/random values for POD according to new()/new.
*1. POD is Plain Old Data. That is a C-like struct. Ctors/Dtors/Funcs. etc make it non-POD.