OpenCV中的const Mat引用有意义吗? [英] Does a const Mat reference in OpenCV make sense?

查看:470
本文介绍了OpenCV中的const Mat引用有意义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下功能中

foo(const Mat& img)

可以在函数中更改

img,而无需编译器发出警告.为什么?这是否意味着const Mat引用没有任何意义?

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?

推荐答案

这是因为Mat包含指向实际图像数据的指针. const仅适用于Mat对象本身(例如,行,列等属性),不适用于指针引用的数据. 注意:即使该功能是

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.

将Mat用作const引用有很多优点. 它告诉程序员一些有关如何使用foo()以及如何修改foo()的知识. 此外,它还会阻止您执行以下操作:

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);
}

这将导致编译器错误.

这篇关于OpenCV中的const Mat引用有意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆