Do the parentheses after the type name make a difference with new?
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.
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 (plain old data), 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 valuenew A()
- zero-initializenew 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 valuenew 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.
Difference between creating object with () or without
The other answers correctly state that the parentheses version is actually a function declaration. To understand it intuitively, suppose you wrote MainGUIWindow f();
Looks more like a function, doesn't it? :)
The more interesting question is what is the difference between
MainGUIWindow* p = new MainGUIWindow;
and
MainGUIWindow* p = new MainGUIWindow();
The version with parentheses is called value-initialization, whereas the version without is called default-initialization. For non-POD classes there is no difference between the two. For POD-structs, however, value-initialization involves setting all members to 0,
my2c
Addition: In general, if some syntactic construct can be interpreted both as a declaration and something else, the compiler always resolves the ambiguity in favor of the declaration.
C++ class initialization with and without parentheses [duplicate]
In the first case the object is left uninitialized, while in the second case the object is guaranteed to be value-initialized, which in this case as the type is POD it means zero-initialized
Where does C++20 prohibit sizeof...T (without parentheses)
It's required by the grammar:
[expr.unary.general]/1
unary-expression:
...
sizeof
...
(
identifier)
...
Related Topics
While Writting Unittests for a Function, Should I Mock the Internal Function Calls Made
Why Should I Not #Include ≪Bits/Stdc++.H≫
What Is the Most Effective Way For Float and Double Comparison
What Are the Differences Between Struct and Class in C++
Is It Better in C++ to Pass by Value or Pass by Reference-To-Const
Why Is My Power Operator (^) Not Working
Function With Same Name But Different Signature in Derived Class
Why Must a Short Be Converted to an Int Before Arithmetic Operations in C and C++
Most Efficient Way of Copying a Raw Byte Array into an Empty Byte Vector
When and Why Will a Compiler Initialise Memory to 0Xcd, 0Xdd, etc. on Malloc/Free/New/Delete
What Exactly Is the "As-If" Rule