c++ 将 const 对象引用传递给函数

2022-01-23 00:00:00 reference constants c++

错误:将 '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.

相关文章