tags:

views:

1800

answers:

6

If 'Test' is an ordinary class, is there any difference between:

Test* test = new Test;
//and
Test* test = new Test();
+8  A: 

No, they are the same. But there is a difference between:

Test t;      // create a Test called t

and

Test t();   // declare a function called t which returns a Test

This is because of the basic C++ (and C) rule: If something can possibly be a declaration, then it is a declaration.

Edit: Re the initialisation issues regarding POD and non-POD data, while I agree with everything that has been said, I would just like to point out that these issues only apply if the thing being new'd or otherwise constructed does not have a user-defined constructor. If there is such a constructor it will be used. For 99.99% of sensibly designed classes there will be such a constructor, and so the issues can be ignored.

anon
Note that this is a particularly important point because the line "Test t(5);" is equivalent to "Test t = Test(5);" -- but "Test t();" is very different from "Test t = Test();". +1
ojrac
-1, I disagree with your statement that the issues can be ignored. You don't have to know the rules precisely, but you should be aware of them in case you have to new a class without a user-defined default constructor (you should then either write the constructor or look up the rules).
avakar
-1 for a known incorrect answer. Your Edit ignores the presence of code written by former C programmers who didn't understand/use constructors.
Tom
+1 for the edit
Joel Coehoorn
+4  A: 

Assuming that Test is a class with a defined constructor, there's no difference. The latter form makes it a little clearer that Test's constructor is running, but that's about it.

Evan Shaw
+1 for the clarification.
Matt Davis
+2  A: 
Test *test = new Test;

is the same as:

Test *test = new Test();

Both create a Test object on the heap.

Be careful if you are creating a Test object on the stack, though:

Test test;

is not the same as:

Test test();

The latter actually declares a function.

Colin
A: 

They are no different, but you should use the parentheses in practice. Otherwise it can be confusing.

cpatrick
What can it be confused with?
anon
Obviously, the OP was a bit confused... For an experienced programmer you are correct.
cpatrick
+3  A: 

In general we have default-initialization in first case and value-initialization in second case.

For example: in case with int (POD type):

  • int* test = new int - we have any initialization and value of *test can be any.

  • int* test = new int() - *test will have 0 value.

next behaviour depended from your type Test. We have defferent cases: Test have defult constructor, Test have generated default constructor, Test contain POD member, non POD member...

bb
+72  A: 

Let's get pedantic, because there are differences that can actually affect your code's behavior. Much of the following is taken from comments made to an "Old New Thing" article (http://blogs.msdn.com/oldnewthing/archive/2006/12/14/1285437.aspx).

Sometimes the memory returned by the new operator will be initialized, and sometimes it won't depending on whether the type you're newing up is a POD, or if it's a class that contains POD members and is using a compiler-generated default constructor.

  • In C++1998 there are 2 types of initialization: zero and default
  • In C++2003 a 3rd type of initialization, value initialization was added.

Assume:

struct A { int m; }; // POD
struct B { ~B(); int m; }; // non-POD, compiler generated default ctor
struct C { C() : m() {}; ~C(); int m; }; // non-POD, default-initialising m

In a C++98 compiler, the following should occur:

  • new A - indeterminate value
  • new A() - zero-initialize

  • new B - default construct (B::m is uninitialized)

  • new B() - default construct (B::m is uninitialized)

  • new C - default construct (C::m is zero-initialized)

  • new C() - default construct (C::m is zero-initialized)

In a C++03 conformant compiler, things should work like so:

  • new A - indeterminate value
  • new A() - value-initialize A, which is zero-initialization since it's a POD.

  • new B - default-initializes (leaves B::m uninitialized)

  • new B() - value-initializes B which zero-initializes all fields since its default ctor is compiler generated as opposed to user-defined.

  • new C - default-initializes C, which calls the default ctor.

  • new C() - value-initializes C, which calls the default ctor.

So in all versions of C++ there's a difference between "new A" and "new A()" because A is a POD.

And there's a difference in behavior between C++98 and C++03 for the case "new B()".

This is one of the dusty corners of C++ that can drive you crazy. When constructing an object, sometimes you want/need the parens, sometimes you absolutely cannot have them, and sometimes it doesn't matter.

Michael Burr
I had to look up POD. http://stackoverflow.com/questions/146452/what-are-pod-types-in-c
Derek B.
Sorry about that - I sometimes forget that "POD" might not be understood by all C++ programmers. It is something that is good for every C++ programmer to know, though - there are important distinctions that a C++ compiler can (or must) make for plain-old-data types. Thanks for the pointer to a good description.
Michael Burr
This answer is awesome.
GMan