27 lines
847 B
Markdown
27 lines
847 B
Markdown
|
# Chapter 13. Copy Control
|
||
|
|
||
|
Advanced topic for class constructors/desctructors
|
||
|
|
||
|
Copy Control = operations of following:
|
||
|
|
||
|
* **Copy- & Move- constructor**: define what happens when an object is initialized from another object of the same type
|
||
|
* **Copy- & Move- assignment**: define what happens when we assign an object (class A) to object (class B)
|
||
|
* **Destructor**: define what happens when object of type A destroyed
|
||
|
|
||
|
When Copy Controls are not cleared defined, C++ compiler automatically create them; -> undefined behavior
|
||
|
|
||
|
## 13.1 Copy, Assign, And Destroy
|
||
|
|
||
|
### 13.1.1 Copy Constructor
|
||
|
|
||
|
```cpp
|
||
|
class Foo {
|
||
|
public:
|
||
|
Foo(); // default constructor
|
||
|
Foo(const Foo&); // copy constructor
|
||
|
// ...
|
||
|
};
|
||
|
```
|
||
|
|
||
|
* copy constructor's 1st parameter is a reference to the class type
|
||
|
* copy constructor's other parameters have default values
|