我很新的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