in above code how can i print the returned string. i must return by the reference. can't i do that? program ended with unwxceptional handling and was not able to print sandeep poudel.
The function constructs a temporary std::string object and returns a reference to it. The problem is that as soon as the function ends the temporary object gets destroyed so when it is time to print the string it no longer exists.
Either return a constchar* or return the std::string by value.