The difference between the new object and the parenthesis

Source: Internet
Author: User
in the new object when there is (), there is no (), do not know what this is exactly the difference.

Like what:
[CPP] view plain copy print?   CBase *base = new cderived (); CBase *base = new cdeviced;

CBase *base = new cderived ();
CBase *base = new cdeviced;

Many people say that braces call constructors without arguments, calling default constructors or unique constructors without parentheses. There is a problem with that.

For the custom class type:

If the class does not have a constructor defined (the compiler synthesizes the default constructor) and does not have a virtual function, then Class C = new Class; The resultant default constructor is not invoked, and class C = new Class (), and the default constructor is invoked.

If the class does not have a constructor defined (the compiler synthesizes the default constructor) but has a virtual function, Class C = new Class; and Class C = new Class (), and the default constructor is invoked.

If the class defines a default constructor, then Class C = new Class; and Class C = new Class (), and the default constructor is invoked.

For built-in types:

int *a = new int; the int *a = new int () is not initialized, and the int () will initialize the int space requested to 0.

The difference between the following two statements is that the value in the first dynamic application space is a random value, and the second is initialized with a value of 0: [CPP] view plain copy print?     int *p1 = new INT[10]; int *p2 = new INT[10] ();

int *p1 = new INT[10];  
int *p2 = new INT[10] ();  

Conclusion: Do not use new without parentheses.

[CPP] view plain copy print?    Class a{Public://a () {a=1;}   Public:int A;   };   A *a1=new A;      A *a2=new a (); cout<<a1->a<<endl; Output: -842150451 cout<<a2->a<<endl;   Output 0 A A3; cout<<a3.a<<endl; Run exception, A is not initialized.

Class a{public
:
//a () {a=1;}
Public:
int A;
};
A *a1=new A;
A *a2=new a ();

cout<<a1->a<<endl; Output: -842150451
cout<<a2->a<<endl;//Output 0
a A3;
cout<<a3.a<<endl; Run exception, A is not initialized.

If you add a virtual, such as virtual ~a () {},

The

[CPP] view plain copy print? cout<<a1->a<<endl; Output: -842150451 cout<<a2->a<<endl; Output-842150451

cout<<a1->a<<endl; Output: -842150451
cout<<a2->a<<endl;//output-842150451

-------------The following is a post on the Internet, which may help to understand the above phenomenon-------------------

A class satisfies any of the following conditions:
1. An object that contains a class that has a constructor (including a compiler-synthesized default constructor)
2. If you inherit from some base class, some of the base classes have a constructor (including a compiler-synthesized default constructor)
3. Have a virtual function, or inherit to a virtual function
4. have virtual base class

If the class does not have a default constructor, the compiler will synthesize a default constructor, doing the following
If this class has constructors, the compiler expands all constructors, doing the following
1. Call the constructor of this object
2. Call the constructor of the base class
3. Set the correct virtual function table pointer
4. Set a pointer to a virtual base class object





Put the conclusion first:

Parentheses call a constructor with no arguments, call the default constructor or unique constructor without parentheses, and see that the law of the initialization of requirement C + + at New may be: for classes with constructors, regardless of parentheses, they are initialized with constructors; New without parentheses allocates only memory space, does not initialize memory, and new parentheses are initialized to 0 while allocating memory.

The following code:

#include <iostream>

using namespace Std;

int main ()
{
int *a=new int[1000];
for (int i=0;i<1000;i++) {
a[i]=i+1;
}
Delete[] A;
int *b=new int[1000];
for (int i=0;i<100;i++) {
cout<<b[i]<<endl;
}

return 0;
}

Not initialized, the result of the output is:

9437380

9443448

3

4

5

6

。。。

Visible, the new operator does not initialize memory.

and slightly change the code (add parentheses after new ()):

#include <iostream>

using namespace Std;

int main ()
{
int *a=new int[1000];
for (int i=0;i<1000;i++) {
a[i]=i+1;
}
Delete[] A;
int *b=new int[1000] ();
for (int i=0;i<100;i++) {
cout<<b[i]<<endl;
}

return 0;
}

The output results are:

0

0

0

0

。。

Visible, has been initialized.

=============================================================================

Further thinking:

Define Class A:

Class a{
Public
int A;
A (): A (10) {};
};

Use the statement in the main function:

A *b=new A;
cout<<b->a<<endl;

A *b=new a ();
cout<<b->a<<endl;

The output is 10, and all of the results are initialized.

However, if the constructor of bar A is deleted, the output of the two statements is: random number, 0.

Thus, C + + in new initialization of the law may be: For a class with constructors, regardless of parentheses, are initialized with constructors; if there is no constructor, the new without parenthesis allocates only the memory space, and does not initialize the memory. New with parentheses is initialized to 0 while allocating memory.


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.