I can get no definitive academic answer and testing has proven to be indecisive; and most examples of member initializations use primitives, such as
Rectangle::Rectangle (int x, int y) : width(x), height(y) { }
Im seeking member initializations with references,
Shape::Shape (Rectangle& r) : rect(r) {}
Is the latter initialization of rect(r) using the default copy constructor (of Rectangle) or is it a short form of rect = r
??
Testing has shown me that constructors can be (are) used but i cannot observe the similar behavior for copy constructors
Shape::Shape (string& str) : rect(str) {}
For this purpose, Rectangle has a Rectangle:Rectangle(string&) constructor
Constructors are always used in that syntax. It can't be assignment, because you can only assign to constructed objects, so it'd be running a constructor and then assigning... rest assured, the language won't do something like that.
To help illustrate the difference between constructors and assignment... Rectangle rect = r;
invokes the constructor, not assignment, despite it's appearance. But once rect
is already declared, rect = r;
is assignment. Assignment only makes sense when rect
already exists, which can't happen during the initial construction of that object because it doesn't exist yet.
Also, in your testing, you might be getting confused by Copy Elision.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.