c++ passing a const object reference to a function(c++ 将 const 对象引用传递给函数)
问题描述
错误:将 'const QByteArray' 作为 'QByteArray& 的 'this' 参数传递QByteArray::append(const QByteArray&)' 丢弃限定符 [-fpermissive]
error: passing 'const QByteArray' as 'this' argument of 'QByteArray& QByteArray::append(const QByteArray&)' discards qualifiers [-fpermissive]
因为在作为函数参数传递时使对象为 const 是一种惯例,所以我已经做到了.但现在我得到一个错误!,我不想让函数保持不变,因为我必须将 qbyte 数组中的数据转换为短格式,然后将其附加到另一个数组中.
since it is a convention to make objects const while passing as function arguments i have done it. but now i am getting an error!!, i dnt want to make the function constant as i have to convert data in qbyte array into short and then append it another array.
QByteArray ba((const char*)m_output.data(), sizeof(ushort));
playbackBuffer.append(ba);
我确实需要将这个数组传递到 playbackbuffer;
它在 playbackBuffer.append(ba);
I really need to pass this array into playbackbuffer;
It is giving me an error on playbackBuffer.append(ba);
请帮忙
提前致谢
please help
thanks in advance
推荐答案
这意味着您正在对 const 成员调用非 const 成员函数.据推测,您的 append 函数会修改字节数组.使用 const 引用,您不应该进行修改.
This means you are calling a non-const member function on a const member. Presumably, your append function modifies the byte array. With a const reference, you shouldn't be modifying.
这篇关于c++ 将 const 对象引用传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:c++ 将 const 对象引用传递给函数
基础教程推荐
- C语言实现宾馆管理系统课程设计 2023-03-13
- C++实战之二进制数据处理与封装 2023-05-29
- [C语言]二叉搜索树 2023-09-07
- C语言编程C++旋转字符操作串示例详解 2022-11-20
- centos 7 vscode cmake 编译c++工程 2023-09-17
- 全面了解C语言 static 关键字 2023-03-26
- [c语言-函数]不定量参数 2023-09-08
- C++实现ETW进行进程变动监控详解 2023-05-15
- 带你深度走入C语言取整以及4种函数 2022-09-17
- C语言 详解字符串基础 2023-03-27
