Introduction
Effective C++ by Scott Meyers book review
Introduction
- Copy Constructor
- Used to initialize an object with a different object of the same type
- Defines how an object is passed by value
- Copy assignment operator
- Used to copy the value from one object to another of the same type
class Widget { public: Widget(const Widget& rhs); // Copy Constructor Widget& operator=(const Widget& rhs); // Copy assignment operator }; Widget w1; Widget w2(w1); // invoke copy constructor w1 = w2; // invoke copy assignment operator Widget w3 = w2 // invoke copy constructor
- The parameter w is passed by value, so aWidget is copied into w.
- The copying is done by copy constructor.
- Pass-by-value == “call the copy constructor”
- But Pass-by-reference-to-const is typically a better choice
bool hasAcceptable(Widget w); Widget aWidget; if(hasAcceptable(aWidget))...