Why is it OK to return an object reference inside a function in c++? -


here example website: http://www.cplusplus.com/doc/tutorial/classes2/ know working example. however, don't understand why object temp can returned operator+ overloading function. have made comments besides codes.

// vectors: overloading operators example #include <iostream> using namespace std;  class cvector {   public:     int x,y;     cvector () {};     cvector (int,int);     cvector operator + (cvector); };  cvector::cvector (int a, int b) {   x = a;   y = b; }  cvector cvector::operator+ (cvector param) {   cvector temp;   temp.x = x + param.x;   temp.y = y + param.y;   return (temp);   ***// isn't object temp destroyed after function exits ?*** }  int main () {   cvector (3,1);   cvector b (1,2);   cvector c;   c = + b; ***// if object temp destroyed, why assignment still work?***   cout << c.x << "," << c.y;   return 0; } 

in example don't return object reference, return object value.

object temp in fact destroyed after function exits time value copied on stack.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -