2

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.

iammilind
  • 68,093
  • 33
  • 169
  • 336
JamesWebbTelescopeAlien
  • 3,547
  • 2
  • 30
  • 51

5 Answers5

4

Since, it's operator +() and not operator +=(), you should be creating a temporary and return the same:

date operator + (const date &d) const
{            //  ^^^^ 1         ^^^^^ 2
  date temp = *this;  // copy current object
  ...
  return temp;  // important: -Wall warned you for missing 'return'
}

There are other 2 important changes you can see:

(1) Passing d as const reference; because you don't need another copy

(2) Making operator + as const correct by adding const at end; because you are not going to modify this object

Update: 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?

Community
  • 1
  • 1
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • +1 for using a reference constant parameter. too many people use "stack" non-reference non-pointer arguments for non-predefined types! – vulkanino Feb 01 '12 at 08:27
  • 1
    It might be worth adding that the usual idiom here is 1) to make `operator+` a free function (but this is certainly not necessary unless there are implicit conversions to `date`) and 2) to implement it in terms of `operator+=`. (You've also got several instances of `data` which should be `date`.) – James Kanze Feb 01 '12 at 08:36
  • +1 for the sample code with the correct function prototype for operator+ – nitin_cherian Feb 01 '12 at 08:48
  • @vulkanino if a date is only a few bytes, pass-by-value could be faster – user253751 Mar 06 '14 at 06:05
2

What does the following overloaded operator of date class should return? I see that it returns garbage.

You are not returning anything from the function, and hence the value returned is any randowm value and it is an Undefined Behavior.

You should return an object of the type Date explicitly by using :

return objName;
Alok Save
  • 202,538
  • 53
  • 430
  • 533
2

You're modifying the first operand of +, which is incorrect. Get a copy and return it:

date operator+(date d) const { 
    date r = *this;
    r.day += d.day; r.month += d.month; r.year += d.year;
    return r;
}
Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
0

An object of type date.

You should construct a new date and not do the += of the various components. This is due to you wanting the + operator to behave in the traditional way as the + operator for numbers.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
0

There is no way but create a new object to return. Don't worry about copying temporary object, most compilers would using NRVO here.

Healer
  • 290
  • 1
  • 7