Seen this in a book I'm reading:
Rectangle r(Point(200,200));
Is this the same as:
Rectangle r = Rectangle(Point(200,200));
Seen this in a book I'm reading:
Rectangle r(Point(200,200));
Is this the same as:
Rectangle r = Rectangle(Point(200,200));
In:
Rectangle r(Point(200,200));
you are initializing a Rectangle
object via a constructor that takes a Point
object.
In:
Rectangle r = Rectangle(Point(200,200));
you are constructing a Rectangle
temporary object as above and then passing it to the copy/move constructor of Rectangle
.
If the copy constructor is properly written, then the resulting objects are the same, but one more copy/move constructor would be theoretically called in the latter.
This is not true if the compiler decides to elide the copy, according to §12.8/31:
When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the constructor selected for the copy/move operation and/or the destructor for the object have side eects. In such cases, the implementation treats the source and target of the omitted copy/move operation as simply two dierent ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization.122 This elision of copy/move operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies):
[...]
- when a temporary class object that has not been bound to a reference (12.2) would be copied/moved to a class object with the same cv-unqualified type, the copy/move operation can be omitted by constructing the temporary object directly into the target of the omitted copy/move
[...]
If the contructor that takes a Point
is not marked explicit
, then you could also have the form:
Rectangle r = Point(200,200);