Why compiler doesn’t return error if operator+() member function of date class is not returning anything. if i do
date d1,d2;
date any = d1 + d2;
then d1 + d2 will create a temporary, what does this temporary gets initialized with?
date operator+(date d)
{
day += d.day;
month += d.month;
year += d.year;
}
Note: its just for test purpose only. Not for commercial use or anything.
Since, it’s
operator +()and notoperator +=(), you should be creating a temporary and return the same:There are other 2 important changes you can see:
(1) Passing
dasconstreference; because you don’t need another copy(2) Making
operator +asconstcorrect by addingconstat end; because you are not going to modifythisobjectUpdate: For your updated question, here is a link which answers it.
Why “not all control paths return a value” is warning and not an error?