Hi, I'm writing an exception class but the compiler keeps complaining about it's destructor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
class myexception : public std::exception
{
public:
std::string msg;
virtual const char* what() const throw()
{
return "error blah blah";
}
};
int main()
{
// ...
}
|
error: looser throw specifier for 'virtual myexception::~myexception()'
c:\program files (x86)\mingw\bin\..\lib\gcc\mingw32\4.5.2\include\c++\exception|65|error: overriding 'virtual std::exception::~exception() throw ()'|
|
If I comment out
std::string msg;
it works fine
1 2 3 4 5 6 7 8 9
|
class myexception : public std::exception
{
public:
//std::string msg;
virtual const char* what() const throw()
{
return "error blah blah";
}
};
|
How can I fix the error in the first example without commenting out std::strting msg?
Thanks for help.
Last edited on
The compiler generated destructor does not include the "throw()" I believe, so try defining it like that yourself.
its out of scope !!!!!!!!!!!! its too dangerous !!!!!!!!
you return address of variable that is dead !!!
char *what()
{
return "asdfasdf"; //// ITS DEAD VARIABLE
}
and if works correctly , your compiler is out of service !!
class myexception : public std::exception
{
public:
std::string msg;
virtual const char* what() const throw()
{
return msg.c_str();
}
};
compile correctly
Last edited on
@ahura24: there's no problem since what() returns
const char *
and string literals in C++ are of type
const char *
.
@firedraco: you was right:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
class myexception : public std::exception
{
public:
std::string msg;
virtual const char* what() const throw()
{
return "error blah blah";
}
~myexception() throw()
{
}
};
|
Does this mean that the default constructor is not generated/called when there are no objects to destroy?
Last edited on