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;
}
getImage()
function for handing over the image to the old part? Or should I use something like std::unique_ptr<double> image
?
std::unique_ptr<double> image
, would I need a custom deleter because the unique_ptr manages an array, and not a pointer to double? double* image
and dynamic allocation? Is it recommended to have such a getImage() function for handing over the image to the old part?
If you have use for such function, then sure. A more modern alternative could be to return a span
that encapsulates pointer to the beginning as well as the length of the array.
Or shouldn't I use modern C++ at all and stick to the old style? With double* image and dynamic allocation?
Dynamic allocation is still used with the newer design. It's just hidden inside std::vector
.
Note that std::vector
has been in the C++ standard since the beginning, and managing dynamic array without a container is very old style pre-dating standard C++ (that's over two decades old now) and the STL before that. I don't know good arguments for staying with the old style.
PS There's probably no advantage for using the additional indirection of std::unique_ptr
. It is probably more optimal to store the vector as a member directly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.