When I read about the copy constructor and copy assignment constructor, I realized that both are passing their properties to each other. And both of them are declared implicitly by the compiler (if not defined). So both must exist, to do something useful or not.
Then I tested this code:
#include <iostream> using namespace std; class Test { public: Test () { cout << "Default constructor" << endl; } Test (const Test& empty) { cout << "Copy constructor" << endl; } Test& operator=(const Test& empty) { cout << "Copy assignment operator" << endl; } private: }; int main () { Test a; // Test default constructor Test b (a); // Test copy constructor Test c = b; // Test copy assignment constructor system ("pause"); return 0; }
But it seems that the copy assignment operator is not called at all. I tried with three conditions:
All inclusive. He prints:
// Default constructor // Copy constructor // Copy constructor # Why not prints out "Copy assignment operator"?
The WHitout assignment operator simply copies the constructor. He prints:
// Default constructor // Copy constructor // Copy constructor # Why it printed out even though I didn't define it?
The Whitout copy constructor will simply copy the assignment statement. He prints:
// Default constructor
Only the constructor. He prints:
// Default constructor
Thus, he, like the compiler, did not even worry if I defined the copy assignment operator or not. None of the four examples above display the Copy Assignment Operator. So when he called, does it really exist and make sense?
source share