I'm a beginner in C++ and the resource I'm using says that the following statement d3 = d1 + d2; calls the following:
- The + operator
- The default constructor
- The copy constructor
- Destructor
- Assignment operator
- Destructor
I don't understand why the copy constructor is called when the result is being assigned to a previously declared variable and why 2 constructors are called.
Operators are as follows:
date& date::operator=(const date& other)
{
cout << "Date assignment op" << endl;
if (this!=&other){
day=other.day;
month=other.month;
year=other.year;
}
return *this;
}
date date::operator+(const date& other) const
{
cout << "Date Operator + called" << endl;
date temp;
temp.day=day+other.day;
temp.month=month+other.month;
temp.year=year+other.year;
return temp;
}