Skip to main content
added 151 characters in body
Source Link
Loki Astari
  • 96.7k
  • 5
  • 125
  • 338

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& 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<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;

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& 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;

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& 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<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;
deleted 6 characters in body
Source Link
Loki Astari
  • 96.7k
  • 5
  • 125
  • 338

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 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;

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;

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& 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;
Source Link
Loki Astari
  • 96.7k
  • 5
  • 125
  • 338

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;