Problem
I have an old code base that is processing images. The C++ style is old (before C++11), there are no smart pointers or anything like that. Only a lot of dynamic allocation and raw pointers.
There is always only one actual image that is processed. These images are kept and passed as c-style arrays:
class A {
A(double* image);
}
class B {
B(){ image = new double[10]; };
double* image;
}
class C {
C();
foo(double* image);
}
I need to add a new class that manages and reads in images. And I would like to set up a class like this, because I feel pretty uncomfortable with all this dynamic allocation and would like to use more modern C++.
class NewImage {
NewImage();
double* getImage() { return &image.get()->at(0); };
readImage();
...
private:
std::unique_ptr<vector<double>> image;
}
- Is it recommended to have such a
getImage()function for handing over the image to the old part? Or should I use something likestd::unique_ptr<double> image?- If I would use
std::unique_ptr<double> image, would I need a custom deleter because the unique_ptr manages an array, and not a pointer to double?
- If I would use
- Or shouldn't I use modern C++ at all and stick to the old style? With
double* imageand dynamic allocation?
std::unique_ptr<std::vector>because there are essentially no added benefits to using it over astd::vector>(except maybe that it's nullable). But the use cases for astd::shared_ptr<std::vector>are a bit more common.