C + + cannot explicitly call constructors, and it generates anonymous objects, which is completely different from Java!

Source: Internet
Author: User

Java has super (), but not in C + +. Take a look at this piece of code:

classA { Public: A () {printf ("A () \ n"); } A (intd) {printf ("A (int) \ n"); A (); } //Cannot call constructor like this! Another anonymous object is generated ~a () {printf ("~a \ n"); }     voidDDD () {printf ("ddd \ n"); } };intMainintargcChar*argv[]) {A aaa (9);    AAA.DDD ();    GetChar (); return 0;}


The output is:
A (int)
A ()
~a
Ddd

But if you change it into
A (int d) {printf ("a (int) \ n");}
The output is:
A (int)
Ddd

Fully prove that there is an anonymous object generated and destroyed in the middle! And I don't know if there's an official solution at the moment, but if you want to define the init () function, forget it.

Reference:
Http://www.cnblogs.com/xkfz007/archive/2012/05/11/2496447.html

Some discussions:

http://bbs.csdn.net/topics/80415554

---------------------------------------------------------------------

However, you can explicitly call destructors, as a reference:

http://fpcfjf.blog.163.com/blog/static/55469793201362442256498/

C + + cannot explicitly call constructors, and it generates anonymous objects, which is completely different from Java!

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.