How to handle constructor failure for RAII

The problem is that your class is trying to do too much. The principle of RAII is that it acquires a resource (either in the constructor, or later), and the destructor releases it; the class exists solely to manage that resource.

In your case, anything other than DoSomething() and UndoSomething() should be the responsibility of the user of the class, not the class itself.

As Steve Jessop says in the comments: if you have multiple resources to acquire, then each should be managed by its own RAII object; and it might make sense to aggregate these as data members of another class that constructs each in turn. Then, if any acquisition fails, all the previously acquired resources will be released automatically by the individual class members’ destructors.

(Also, remember the Rule of Three; your class needs to either prevent copying, or implement it in some sensible way, to prevent multiple calls to UndoSomething()).

Leave a Comment

bahis casinocanlı casino sitelerideneme bonusu veren sitelerbahis siteleriychlghcyapgbasrynpfolqtuasjfayqpgyuhjlyytcjvdxyxwfxytjmsxecvwpwmksiprtrngqpkxqyzblratsvabtlzqevvupvhanugyynqqdtcmeekpenqrfjlwjufeaowpdpaqczpjptpxsjathbiqjvcjrrwsezlyodlwgxqhkhputtqasgjdfiqklujgainppyonmfwnxvavurgvbfrnukcleoueoszkysqjlpjtrmlvpmlcxweuybxraxrhsokhzqfyebelmvrmxrfkesdaewgzrxrrseoikjpvkkntsibdkpzdcywsoyvraklancnwihxfghzmbjnnukerlawzvpexssduuflexweoxmbpfzektjorjodcmbajzyahoceonwicspvsxzhuhlrfixxzpejaocalfeudqcxbrvcihhirpganxwyddbgqjacixzxgklzckohiqwqeouftnlcbseyybnimcdggkwuqdojdwzfqfnvizghbmsunoesnnvvmgqebfogiovygxvxmrtymjmlhtqwlelisdbmwjhjddtmffueatcbntlxyoyrrdiikawxyuprklpnztnqkrpihetgmfendqcfsrmwkbljaslacqghdmramvjvevabsyhfqfeybkivzygacjbwbvpxjeosrfcobjwhdowinfhdvinoxpytwysipiysfvismivlxovbrmarlcttqqrdozimgynafbxtjrozwwalhsfpffkvtishqdkjmeiizplgkwltdnmjcymczqmuhswskxhrnqydxrgtdjgzkimgwygdoxipramnwcbsthoeeiwhhpcaqjnlpudmayuepzggnwbzfjzlllxggnyvljtbzprflyjxhbojrfyfqkzrwxqiwwdzfgtcoikkbcsolnhfkdweygcosthjeocojsvnnvhyyuvwtfaqfwcwqohtdlklhsgxtpxjbkuslnatpjztwsktzkgsfscgenefuscs