
-----------------------------------
klopyrev
Wed Mar 07, 2007 1:51 am

Continued confussion with C++
-----------------------------------

// convert rectangular to polar coordinates
polar rect_to_polar(rect xypos)
{
     polar answer;
     
     answer.distance = 
            sqrt( xypos.x * xypos.x + xypos.y * xypos. y);
     answer.angle = atan2(xypos.y, xypos.x);
     return answer;
}


Assuming the two structures used in that method are defined, wouldn't that method not work properly since the method returns a local variable that would cease to exist when the method returns?

KL

-----------------------------------
wtd
Wed Mar 07, 2007 2:29 am

RE:Continued confussion with C++
-----------------------------------
No.  

The function works fine (assuming the struct types are properly defined).  The struct is returned by value, rather than reference.

-----------------------------------
klopyrev
Wed Mar 07, 2007 2:35 am

Re: Continued confussion with C++
-----------------------------------
Thanks for the response. I figured it out on my own after rereading the chapter about functions.

KL
