As far as I know that local variables are popped from the stack when its scope is done. But in the following code, this information isn't provided. The code is running properly, although local variable is popped from the stack.
Here are the code:
result = c1 + c2; // There is not any problem, why?Code:#include <iostream> using namespace std; struct ComplexNumber{ int realNo; int imaginaryNo; }; ComplexNumber operator +(ComplexNumber &cN1,ComplexNumber &cN2) { ComplexNumber temp; // LOCAL VARIABLE temp.realNo = cN1.realNo + cN2.realNo; temp.imaginaryNo = cN1.imaginaryNo + cN2.imaginaryNo; return temp; // LOCAL VARIABLE IS RETURN ? } int main() { ComplexNumber c1; ComplexNumber c2; ComplexNumber result; c1.realNo = 1; c1.imaginaryNo = 2; c2.realNo = 3; c2.imaginaryNo = 4; result = c1 + c2; // There is not any problem, why? cout << "result.realNo = " << result.realNo << endl; cout << "result.imaginaryNo = " << result.imaginaryNo << endl; return 0; }