I’m trying to understand the constructor used in a class implemented in a library I am using. The key components of class SequenceAnalyzer look like:
class SequenceAnalyzer {
protected:
std::vector<cv::Mat> images_;
public:
SequenceAnalyzer( std::vector<cv::Mat> *images = NULL )
{
if (images != NULL)
images_ = (*images);
}
};
When constructing an instance of this class in my main, I pass it a reference to a vector:
std::vector<cv::Mat> myImages;
SequenceAnalyzer se(&myImages);
Now passing in my images by reference passed their location in memory to the class. But my understanding of the (*images) operator means that their address has been dereferenced and so the = operator then copies the contents.
Is there any advantage in passing myImages into the class in this manner? Why are pointers used in the first place if it doesn’t end in saved copying overhead anyway?
There isn’t any advantage. I would write the same code as:
Now I don’t need to worry about pointers: