Ask Your Question

Why does InputOutputArray take a const cv::Mat& reference for creation?

asked 2018-02-02 14:06:28 -0500

wilsonharron gravatar image

updated 2018-02-02 16:27:10 -0500

The constructor for an InputOutputArray takes a const cv::Mat& reference in the constructor. I'm wondering why this call doesn't fail:

void this_shouldnt_compile(const cv::Mat& mat) {
//const, so the header should not be modifiable (I realize the data is)
      putText(mat, "some text", cvPoint(0,10), 
                   FONT_HERSHEY_COMPLEX_SMALL, 0.8, 
                   Scalar(0,255,0), 1, CV_AA);
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-02-02 17:04:04 -0500

updated 2018-02-02 19:30:23 -0500

It's a parameter type declaration, not a constructor. In the putText function, the Mat header is unchanged. Only the data pointed to by the Mat data is changed. So, the Mat header is constant.

Sharing the content data array between several Mat headers is intentional to the design of OpenCV. See the tutorial Mat - The Basic Image Container.

Const is a promise that the elements of the referenced data will not be modified. This is upheld. The Mat header includes a pointer to the data array. No part of the header is modified.

The content of the data array that the header points to is modified. That is not part of what is promised to remain constant. It cannot be protected using const Mat.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-02-02 14:04:50 -0500

Seen: 134 times

Last updated: Feb 02 '18