New Create an object, do I need braces?

Source: Internet
Author: User
Tags scalar

Original link: http://stackoverflow.com/questions/620137/do-the-parentheses-after-the-type-name-make-a-difference-with-new/

Q: Suppose you have class Test, what's the difference between the following two ways of writing?

Writing 1, test* test = new test;

Writing 2, test* test = new test ();

A: This requires meticulous research, because the difference between the two types of writing is subtle, but it can lead to different running results. Whether the test is a pod type, whether it contains pod members, and the default constructor generated automatically by the compiler, is related to whether the new allocated memory is initialized.

In the c++98 standard, there are two types of initialization, zero initialization and default initialization. (concept, terminology, do not know how to translate, retain the original English)

In the C++03 standard, there is also the addition of a third, that is, value initialization.

The difference between the above three kinds of initialization, see article 2nd.

Consider this piece of code:

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 the c++98 standard, it would be this:

The value of new a-m is not determined
The value of new A ()-M is zero
New B-default construct (m not initialized)
New B ()-default construct (m not initialized)
New C-default construct (m initialized to 0)
New C ()-default construct (m initialized to 0)

In the C++03 standard, it would be this:

The value of new a-m is not determined
New A ()-Performs value initialization, because A is a pod type, so actually executes zero initialization and the result m is initialized to zero
New B-The default initialization,m was not initialized
New B ()-Performs value initialization because the constructor is generated automatically by the compiler (not by the user), so zero initialization is executed for all members, and the result m is initialized to zero
New C-Proceed to default initialization, call constructor, M initialize to zero
New C ()-Perform value initialization, call constructor, M init to zero

In combination, there are two places to look at:

1, in the two C + + standards, for pod type struct A, the results of new A and new A () will be different. Writing parentheses will assign the members of the struct to zero, without the parentheses.

2. For new B (), the behavior of the C++98 Standard and the C++03 standard is inconsistent. The former does not initialize the member variable to zero, and the latter will.

This is one of the dark corners of C + +, and careless words may make you mad. When you create an object with new, you sometimes need braces, sometimes not parentheses, and sometimes no parentheses.

Nasa

1, after visual Studio 2008 test, int* p1 = new int; int* P2 = new int (), the *P1 is an indeterminate value, and *p2 equals zero.

2, what is called the default initialization, what is called the value initialization, what is called zero initialization, there is also an article. Http://stackoverflow.com/questions/7084831/difference-between-default-initialize-and-value-initialize-in-c03. Roughly translated as follows:

Definition of zero initialization: for scalar types (scalar type), initialized to zero, and for Non-union class (or struct) type, each member and each base class is zero Initialization (recursive definition); For an array, each member is zero initialization (recursively defined); For union, the first member is zero initialization; For a reference, no action is taken.

Definition of the default initialization: For the non-pod type, the call to the defaults constructor (error if the default constructor is inaccessible); For an array, each member is default initialization (recursively defined); See as the same as zero initialization.

The definition of value initialization: If there is a user-defined constructor, the default constructor is invoked (an error occurs if the default constructor is inaccessible), and there is no user-defined constructor for the Non-union class (or struct) type. Each member and each base class is value initialization, and for an array, each member carries value initialization, and other cases are treated as consistent with the zero initialization.

3, the translation has a few omissions, only recorded I think the more important content. The main role of translation here is: (1) Memo, (2) Let more people know, (3) Let us read faster. But if you need an accurate understanding, it is advisable to read the original text.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.