c++ - 通过 const& 和通过 & 将对象传递给同一个函数

标签 c++ const-correctness

我有一个图像函数声明为

thresholding( const Image &imgSrc, Image &imgDest );

如果我这样做会发生什么;

Image img;
tresholding( img, img );

这是明确的吗?因为在这种情况下 img 发生了变化。

P.S: threshold 读取 imgSrc 并且如果 `imgSrc[i] < lowerThr -> imgDest[i] = 255 else imgDest[i] = 0

更准确地说:

__m128i _mm_src = _mm_load_si128 ( any_value );
__m128i _mm_src = _mm_load_si128 ( (__m128i*) &imgSrc[0] );
__m128i _mm_dest = _mm_load_si128 ( (__m128i*) &imgDest[0] );


_mm_dest = mm_cmpgt_epi16 (a, thr);

最佳答案

对一个对象有多个可能不同类型的引用(就像有多个指向同一个对象的指针一样)没有问题。函数的行为也可能很好:像这样的表达式

imgDest[i] = (imgSrc[i] < lowerThr) * 255;

为引用同一对象的 imgSrcimgDest 定义明确。排序 o.s. 没有问题发生。不过,您应该检查文档或源代码以了解 threshold,以确保它的实现方式可能要求 imgSrc 在整个执行过程中保持不变。您不应该在不了解实现的情况下做出假设。

关于c++ - 通过 const& 和通过 & 将对象传递给同一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26956733/

相关文章:

c++ - 如何在文件中查找特定单词周围的单词

c++ - 二维数组,IF条件检查

pointers - D: 似乎无法创建一个 std.container.Array of const struct pointers

C++ 避免 const 和非常量访问的代码重复

c++ - 为什么 std::num_put 通过非 const 引用获取 ios_base 参数?

c++ - 链接器错误 - 其他包含目录问题?

C++:如何获取用户输入并将其放入井字棋盘

c++ - boost::interprocess scoped_allocator 和不在共享内存中的容器的容器

c++ - 资源句柄常量正确性

iphone - NSString "initWithBytesNoCopy:length:encoding:freeWhenDone:"可以修改输入 "bytes"吗?