I'm trying to return a string from a function like this:
virtual const char* what() const throw()
{
std::string str = "Name Error: name ";
str+= n_name;
str += " is not defined";
char ret[256]="";
const char* temp = str.c_str();
strcpy(ret, temp);
return ret;
}
But when I try to print it in another place:
const char* str= exc.what();
std::cout << str;
The output is garbage.
I thought the problem was the char[] and it should be char*, but when I changed it to
char* ret="";
the program crashed at
strcpy(ret, temp);
Can someone please help me? P.S. the function has to return a const char*