Zdravim,
uvazujme nasledujuci kod:
#include <iostream>
#include <exception>
using namespace std;
class fooException : public exception
{
public:
const char * what() const throw()
{
return 0;
}
~fooException() throw() {}
};
class options
{
public:
options(int argc, char ** argv)
{
printVersion();
}
void printVersion()
{
cout << "1.0.0\n";
throw fooException();
}
};
class foo
{
private:
options * m_options;
public:
foo(int argc, char ** argv)
{
// ...
m_options = new options(argc, argv);
// ...
}
~foo()
{
delete m_options;
}
};
int main(int argc, char ** argv)
{
try
{
foo f(argc, argv);
} catch (fooException & e) {}
return 0;
}
v skratke mam 2 triedy - prva je "hlavna", druha je ta ktora spracuvava agrumenty z terminalu. V hlavnej triede pouzivam na vytvorenie objektu triedy options operator new tj. v destruktore tu alokovanu pamat musim uvolnit. Ak by uzivatel zadal napr nieco taketo:
$ ./foo -v
tak by mal dostat takyto vystup:
1.0.0
s tym, ze program hned po vypisani verzie zacne "backtrace-ovat", zavola destruktory oboch tried a ukonci sa. Je taketo riesenie pomocou vynimky spravne alebo odporucate nieco ine?
Diky