Does a const Mat reference in OpenCV make sense?

拈花ヽ惹草 提交于 2019-12-01 23:48:04

问题


In the following function

foo(const Mat& img)

img can be changed in the function without even a warning by the compiler. Why? Does it mean const Mat reference don't make any sense?


回答1:


That is because a Mat contains a pointer to the actual image data. The const applies only to the Mat object itself (e.g. attributes like rows, cols) and not to the data referred to by the pointer. Note: even if the function was

foo(Mat img)

you could still change the image data.

There are a number of advantages to passing a Mat as const reference. It tells programmers something about how to use foo () and how to modify foo(). Also, it stops you doing things like:

void foo(const cv::Mat& img)
{
    img.create(5, 6, CV_8UC3);
}

This will get a compiler error.



来源:https://stackoverflow.com/questions/16771905/does-a-const-mat-reference-in-opencv-make-sense

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!