c++ - Difference between A* pA = new A; and A* pA = new A(); -


In C ++, what is the exact difference between the following dynamic object creations:

  A * PA = New A; A * PA = New A ();  

I did some tests, but it seems that in both cases, the default constructor is called and only this. I am looking for any difference in performance ...

thanks

If A is a POD-type, then New A will allocate a new A object but leave it with an indefinite value, otherwise New code will be new default initialize new object.

In all cases, new A () start the value new A object.

This is clearly different behavior for POD types, but non-POD affects non-union class types without an experiment declared constructor.

Example

struct A {int a; Std :: String S; };

A is a non-POD class type without a user-defined constructor when a A is default starting which is defined defined constructor which calls the default constructor for s (a non-POD type), but A is not initiated .

When someone is A the value is initially , because its use is not a declared constructor, all its members are started which means that the default constructor for s is called and a is zero initialized .

ISO 14882: 2003 References:

  • 5.3.4 [expr.new] / 15: How a new allocated by expression Whether the items have been abandoned or not, besides a pair of parentheses or otherwise. 8.5 [dcl.init] / 5: It means start zero , default start and start the price .

  • 12.1 [class.ctor] / 7,8: Form is a user-written constructor that matches the definition of a defined default constructor. 12.6.2 [class.base.init] / 4: How are the members of the consortor initiated the chairs and members not listed in the initial list.


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -