I would have thought that throw() would just throw the same object, but it seems the copy constructor (which the compiler automatically adds in) is being called. Since you are just new()-ing the string memory when your exception class is constructed and delete()ing it on destruction, you would do better to use a fixed array.
The best solution in this case seems to be to store the const char* you hard-coded (without copying it) and that''ll last until after main() has returned. So although the pointer will be copied by an invisible copy constructor, the data will remain in memory until the program exits.
So it would be perfectly fine to write it this way:
class Exception
{
const char *error_string;
Exception(char *string) // constructor
: error_string(string) // just copy the pointer
{
}
};
I have to go now, so if anything wasn''t clear just post it and I''ll try to explain better.
-
null_pointerSabre Multimedia