Answer the question
In order to leave comments, you need to log in
How to organize dependency injection and template usage in C++?
There is something like this code:
struct Object {}
template<class Img>
class IImageRecognizer {
public:
virtual ~IImageRecognizer() {}
virtual Object recognize(Img* img) = 0;
}
template<class Img>
class OpenCVRecognizer: public IImageRecognizer<Img> {
public:
Object recognize(Img* img);
}
class App {
public:
App(IImageRecognizer<cv::Mat>* recognizer);
private:
IImageRecognizer<cv::Mat>* recognizer;
}
...
IImageRecognizer<cv::Mat>* recognizer = new OpenCVRecognizer<cv::Mat>();
App* app = new App(recognizer);
Answer the question
In order to leave comments, you need to log in
Templates are not needed here. You can introduce your own UniversalImage image class, which automatically converts images between all used formats and produces the desired one:
class UniversalImage {
boost::variant<cv::Mat, AnotherImage> internalImg_;
public:
UniversalImage(const cv::Mat& img);
UniversalImage(const AnotherImage& img);
cv::Mat asCVMat() const;
AnotherImage asAnotherImage() const;
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question