Re: How to make this exception-safe
On 22 Nov, 00:54, "Thomas Beckmann" <ka6552-...@online.de> wrote:
V:V() : v_(3u)
{
try {
v_[0u] = new C(2);
v_[1u] = new C(1);
v_[2u] = new C(3);
} catch {
delete v_[2u];
delete v_[1u];
delete v_[0u];
throw;
}
}
What's wrong with this? Looks solid, easily readable and thus maintainable
to me.
Nothing wrong with it really, I just prefer avoiding try/catch blocks
with manual resource management whenever an RAII solution is
feasible.
Mostly making a templated pointer container available in your code base pays
off shortly when dealing with non-value types.
Sorry, it is essential that it is written entirely in Standard C+
+(03).
In what way is a smart pointer or a pointer container non-Standard C++?
Of course they are.
However, in this particular case, the ideal solution is the one which
leads to the smallest net increase in code. For a very small number of
objects, that would be my solution or yours (adding 2n lines of code
for n objects). For a slightly larger number, some of the other
solutions will scale better, requiring n + c LoC for n objects, or
even c, like Yechezkel's solution.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]