Copy constructor vs. Assignment Operator
- A copy constructor creates. anew object.
- An assignment operator assigns a value to an existing object.
- An assignment operator is always called on an object that has already been constructed.
Assignment Operator
- The C++ operator automatically assigns one if not by a programmer
Custom Assignment Operator
- is a public member function of the class.
- has the function name operator=.
- has a return value of a reference of the class's type.
- has exaclty one argument.
- looks like
//↓ returns the class by reference "Cube&"
//↓ has only one parameter "Cube& obj"
Cube& cube::operator=(const Cube &obj) {
length_ = obj.length_;
return *this; // returns an instance of the class
}
'정규 강의 > Object-Oriented Data Structures in C++' 카테고리의 다른 글
C++ Copy Constructor (0) | 2019.10.03 |
---|---|
C++ Class Destructors (0) | 2019.10.03 |
C++ Pointers (0) | 2019.10.01 |
1.2 C++ Classes (0) | 2019.09.26 |