我正在阅读 OpenCV 教程,它对 OpenCV 的图像持有者类(cv::Mat)说了以下内容:
The cv::Mat class implements reference counting and shallow copy such that when an image
is assigned to another one, the image data (that is the pixels) is not copied, and both images
will point to the same memory block. This also applies to images passed by value or returned
by value. A reference count is kept such that the memory will be released only when all of the
references to the image will be destructed.
我特别感兴趣的部分是This also applies to images passed by value or returned.
如何在按值传递时指向同一个内存块?我觉得这与=
运算符重载有关。但是它说即使返回了图像,它也只会返回指向同一内存块的图像,而不会创建新的图像。我不明白他们是如何实现这一点的。
但这是我所理解的:
鉴于按值传递并返回图像使新图像共享相同的内存块,因此实现引用计数是有意义的。
但是你能解释一下内存块是如何共享的,即使对象是通过值返回或传递的吗?