Quantcast
Viewing all articles
Browse latest Browse all 17

Answer by Mark Lakata for Does C++ support 'finally' blocks? (And what's this 'RAII' I keep hearing about?)

As many people have stated, the solution is to use C++11 features to avoid finally blocks. One of the features is unique_ptr.

Here is Mephane's answer written using RAII patterns.

#include <vector>#include <memory>#include <list>using namespace std;class Foo{ ...};void DoStuff(vector<string> input){    list<unique_ptr<Foo> > myList;    for (int i = 0; i < input.size(); ++i)    {      myList.push_back(unique_ptr<Foo>(new Foo(input[i])));    }    DoSomeStuff(myList);}

Some more introduction to using unique_ptr with C++ Standard Library containers is here


Viewing all articles
Browse latest Browse all 17

Trending Articles