Ioannis Vranos wrote:
Flzw wrote:
Yes, thanks, just read some things about that, but, what happens to
the members variables ? are they destroyed properly ?
If you have allocated objects in the free store using new beforehand
within the constructor (usually a bad idea to do it in this way),
make sure to clean them up before throwing the exception.
That's one example of cleanup you'd have to do. Furthermore, if you had
C-style FILE*'s, you would need to fclose() them, etcetera. Basically,
you would do whatever you would normally do in the destructor.
The easiest way is to use C++-style members: std::fstream will clean
up itself unlike fclose(FILE*), std::string will clean up instead of
free(char*), etcetera. If you have another resource which needs
C-style cleanup (e.g. from an legacy API), you can wrap that
resource in a C++ class, with a dtor dedicated to this cleanup
(many early std::fstream implementations were such wrappers around
FILE*)
The trickier way is to write a function try-block which wraps
the entire ctor (including member initializers). Check your favorite
C++ book for that.
Regards,
Michiel Salters