RAII and code duplication in constructor/destructor I have a class hierarchy similar to this:  

struct VideoCapture { virtual open(const string&amp; uri) = 0; virtual close() = 0; };
struct VideoCapture1 : public VideoCapture {...};
struct VideoCapture2 : public VideoCapture {...};
struct VideoCapture3 : public VideoCapture {...};


I added the following constructor and destructor in each of these classes:

VideoCaptureN(const string&amp; uri) { open(uri); }
~VideoCaptureN() { if (!isOpen()) { close(); } }


Though, providing RAII this did not pass the code review as I had to duplicate the same exact code in every classes.

What would be the solution to get RAII and avoid code duplication?
You can't call virtual functions in constructors and should not call them in destructors (you may have already destroyed state that the virtual function relies on).

You can use the PIMPl pattern to help you out though (like the c++ stream, they use a wrapper class and a buffer class that does the work).

class VidCaptureInterface
{
    public:
    virtual ~VidCaptureInterface() {}   
    virtual open(string const&amp; uri) = 0;
    virtual close()                 = 0;
};

// When C++17 comes along then
// You may be able to force T to be a VidCaptureInterface
// This is one solution. But you basically want interface
// to be derived from `VidCaptureInterface`. In C++ stream
// the buffer is held as a pointer and can be re-assigned
// So alternatives include using some form of re-assignable pointer.
template&lt;typename T&gt;
class VideoCapture
{
     T   interface;
     public:
         VideoCapture(std::string const&amp; uri)
         {
             interface.open(uri);
         }
         ~VideoCapture()
         {
             if (isOpen())
             {
                 interface.close();
             }
         }
};

typedef VideoCapture&lt;VidCaptureInterface1&gt;  VideoCapture1;
typedef VideoCapture&lt;VidCaptureInterface2&gt;  VideoCapture2;
typedef VideoCapture&lt;VidCaptureInterface3&gt;  VideoCapture3;