The answer has already been given here:
What is the difference between an assignment operator and a copy constructor?
So, the fact is that:
A copy constructor is used to initialize a previously uninitialized object from some other object data.
Assignment operator used to replace data from a previously initialized object with other object data.
Here is an example:
#include <iostream> using namespace std; class MyClass{ public: MyClass(){ cout << "Default ctor\n"; } MyClass(const MyClass& copyArg){ cout << "Copy ctor\n"; } MyClass(MyClass&& moveArg){ cout << "Move ctor\n"; } void operator=(const MyClass& assignArg){ cout << "Assignment operator\n"; } bool operator<(const MyClass& comparsionArg) const { return true; } }; template<class T> T max(const T* array, int size) { T result = array[0]; for (int i = 0; i < size; ++i) { if (result < array[i]) { result = array[i]; } } return result; } int main(){ MyClass arr[1]; const MyClass& a = max(arr, 1); return 0; }
To find out what exactly is going on, we need to compile with -fno-elide-constructors .
Output:
Default ctor Copy ctor Assignment operator Move ctor
So, the default constructor is called on this line for one element of the array:
MyClass arr[1];
Then we initialize the previously uninitialized object and the copy constructor :
T result = array[0];
Then we assign a previously initialized object and an assignment operator :
result = array[i];
After we need to create an object outside the scope, as we return by value and for this constructor move :
return result;
Then bind the object built with the move constructor in the main scope to const link:
const MyClass& a = max(arr, 1);
toozyfuzzy
source share