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 stream operators, they use a wrapper class and a buffer class that does the work).
class VidCaptureInterface
{
public:
virtual ~VidCaptureInterface() {}
virtual open(string const& 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`.
template<typename T>
class VideoCapture
{
T interface;
public:
VideoCapture(std::string const& uri)
{
interface.open(uri);
}
~VideoCapture()
{
if (isOpen())
{
interface.close();
}
}
};
typedef VideoCapture<VidCaptureInterface1> VideoCapture1;
typedef VideoCapture<VidCaptureInterface2> VideoCapture2;
typedef VideoCapture<VidCaptureInterface3> VideoCapture3;