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