If I dont define my own constructor is there any difference between Base *b = new Base; vs Base *b = new Base(); ?
views:
187answers:
3EDIT: SEE @David 's ANSWER -- THIS IS WRONG BUT I CANNOT DELETE IT BECAUSE IT IS ACCEPTED
There is no difference in either case -- it doesn't matter if you define your own constructor or not.
The only difference is that for primitive types (i.e. int or float), adding the () will initialize the value to zero. (Demonstration on Codepad)
See this example (Output is on codepad)
#include <iostream>
struct ConstructorChecker
{
ConstructorChecker()
{
std::cout << "Hey look! A new constructor checker!" << std::endl;
}
};
struct BasicClass
{
};
int main()
{
//Note constructor called in both cases.
ConstructorChecker *one = new ConstructorChecker;
delete one;
ConstructorChecker *two = new ConstructorChecker();
delete two;
//Same deal -- just because the compiler provides the constructor doesn't mean
//it behaves any differently.
BasicClass *basic = new BasicClass;
delete basic;
BasicClass *basic2 = new BasicClass();
delete basic2;
return 0;
}
As Billy mentioned, both are one and the same.
This is called the 'value initialiation syntax' ($8.5/7).
An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.
Initialization is kind of a PITA to follow in the standard... Nevertheless, the two already existing answers are incorrect in what they miss, and that makes them affirm that there is no difference.
There is a huge difference between calling new T and new T() in classes where there is no user defined constructor. In the first case, the object will be default-initialized, while in the second case it will be `value-initialized*. If the object contains any POD subobject, then the first will leave the POD subobject uninitialized, while the second will set each subelement to 0.
struct test {
int x;
std::string s;
};
int main() {
std::auto_ptr<test> a( new test );
assert( a->s.empty() ); // ok, s is string, has default constructor
// default constructor sets it to empty
// assert( a->x == 0 ); // this cannot be asserted, the value of a->x is
// undefined
std::auto_ptr<test> b( new test() );
assert( b->s.empty() ); // again, the string constructor sets to empty
assert( b->x == 0 ); // this is guaranteed by *value-initialization*
}
For the long road... default-initialize for a user defined class means calling the default constructor. In the case of no user provided default constructor it will call the implicitly defined default constructor, which is equivalent to a constructor with empty initialization list and empty body (test::test() {}), which in turn will cause the default initialization of each non-POD subobject, and leave all POD subobjects uninitialized. Since std::string has a user (by some definition of user that includes the standard library writer) provided constructor, it will call such constructor, but it will not perform any real initialization on the x member.
That is, for a class with user provided default constructor, new T and new T() are the same. For a class without such a constructor, it depends on the contents of the class.