New object without parenthesis (c + + object)

Source: Internet
Author: User

Broad rhetoric: Braces call constructors without arguments, calling default constructors or unique constructors without parentheses.

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

(Compiler-Synthesized default constructor:

Build: If no constructors are explicitly defined in a user-defined class, the compiler automatically generates a default constructor for that type, called a synthesized constructor (synthesized default constructor).

Call: If you define a variable for a class without providing an initializer, the default constructor is used

Exception: 1. A class explicitly declares any constructor, and the compiler does not generate a public default constructor. In this case, if the program needs a default constructor, it needs to be provided by the designer of the class. 2. A class declares a default constructor that is not public, and the compiler does not generate a public default constructor. In short, the compiler does not generate a default constructor after the constructor has defined that the constructor is declared (public or private). )

If the class defines a default constructor, then class C = NW clases; 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] ();

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.

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

-------------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

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.