Bird's eye view : Following code should generate error when compiled.
#include <iostream>
using namespace std;
class Soap{
int r;
public:
Soap(){
r = 0;
}
Soap operator+(Soap &s){
Soap new1;
new1.r = r + s.r;
return new1;}
Soap& operator=(const Soap &s){
r= s.r;
}
};
int main(){
Soap p,q,r;
r = p +q;
}
Acc. to the code my Operator= should return something of type Soap. But as I did not include any return statement in the code, the operator= should have returned void. But then it would have generated an error saying non-void fn. is returning void (while compiling). But no such error is genrated.
This means that the gcc is returning Something of type Soap here. But what is it returning??? And how's it doing it without even me telling it ?
Edit : As my q is being compared to the q - Why does flowing off the end of a non-void function without returning a value not produce a compiler error? I will make my Q more clear.
While running the code when operator= is called by the compiler (i.e. when the statement r = p+r is executed) the function returns a soap type. I never typed return statement in the fn operator=, leaving aside the fact that during actual running a soap type is being returned !!! So what is gcc doing here ? Is it initialising and returning a Soap object even without me typing it ?