I found the following piece of code somewhere and I was wondering if it is legal or not in C++. ret variable is stack variable, and once foo returns the memory allocated to ret no longer exists. But string is a class, and I think copy constructor is called to copy the contents of ret to var. Is this true? is the following piece of code valid?
    string foo(int x)
    {
        string ret; 
        //some operation on ret
        return ret; 
    }
    string callingFunc()
   {
        string var = foo(2); 
        // some operation on var
   }
 
     
    