C + +-an issue with constructors called constructors

Source: Internet
Author: User

Today, C + + experiments, the topic is to write a two-dimensional point of the class, and then let a three-dimensional point of the class inherit it and then expand. Topics are common examples of object-oriented languages in general.

Then comes the question: when you write a construction method with Java, if you need to call another constructor of the same class using a constructor method, we recommend that we write (to write a two-dimensional point class for example):

/*********** Java *************/ Public classpoint2d{Private Doublex; Private Doubley;  Publicpoint2d () { This(0,0); }         PublicPOINT2D (DoubleXDoubley) {         This. x=x; This. y=y; }        /************getter && setter************/}

Then in C + + it is not possible to call POINT2D (double x,double y) in this way 0,0.

If you write this in C + +:

/*********** C + + *************/classpoint2d{ Public: Point2d () {point2d::P oint2d (0,0); } point2d (DoubleXDoubley) {         This->x=x;  This->y=y; }        /************getter && setter************/    Private:    Doublex; Doubley; }

Then you can still be wrong, because in point2d () This constructor, what we do is call POINT2D (0,0) to generate an anonymous object, and then nothing is done. So if you initialize an object with POINT2D (), checking the value of the object will reveal that the object's X and Y are not initialized.

If you want to call the constructor like Java, you can write this:

/*********** C + + *************/classpoint2d{ Public: Point2d () {New( This) POINT2D::P oint2d (0,0); } point2d (DoubleXDoubley) {         This->x=x;  This->y=y; }        /************getter && setter************/    Private:    Doublex; Doubley; }

In fact, this technique has a name placement new, which is different from the general operator new. The explanations are as follows:

New (pointer) constructor () inside, look at the parentheses inside a pointer the whole sentence means to generate an object and then place the object at the address pointed to by the pointer. This method can construct the object anywhere.

Find relevant information on the Internet: Http://stackoverflow.com/questions/22604598/what-does-new-this-mean

C + +-an issue with constructors called constructors

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.