我很新的C++,並想知道是否有可能做這樣的事情:是否有可能直接在C++中的另一個構造函數的參數中構造一個對象?
Rectangle rect(Point(0, 0), 10, 10); // doesn't work
的想法是,矩形採用Point對象以及寬度和高度參數。該構造是這樣的:
Rectangle::Rectangle(Point & point, double width, double height) {
this->point = point;
this->width = width;
this->height = height;
};
Point::Point(double x, double y) {
this->x = x;
this->y = y;
};
我可以這樣得到預期的效果:
Point point(0, 0);
Rectangle rect(point, 10, 10); // this works
,但我認爲這將是很好,如果我能在參數直接實例我點新長方形。如果這是可能的,請讓我知道!謝謝!
通過價值使它成爲'常量點和point' – GeorgeAl
走,第一個應該管用。 – tkausl
出於某種原因,我認爲'point'超出了範圍。我錯了。 – tkausl