c ++将const对象引用传递给函数 [英] c++ passing a const object reference to a function

查看:26
本文介绍了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 数组中的数据转换为 short,然后将其附加到另一个数组.

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 成员调用非常量成员函数.据推测,您的 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对象引用传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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