Does a const Mat reference in OpenCV make sense?

前端 未结 1 900
北荒
北荒 2021-01-21 08:03

In the following function

foo(const Mat& img)

img can be changed in the function without even a warning by the compiler. Why?

相关标签:
1条回答
  • 2021-01-21 08:48

    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.

    0 讨论(0)
提交回复
热议问题