Argument type for Qt signal and slot, does const reference qualifiers matters?

不想你离开。 提交于 2019-11-27 13:11:32
e8johan

Qt checks a normalized signature, meaning

Normalization reduces whitespace to a minimum, moves 'const' to the front where appropriate, removes 'const' from value types and replaces const references with values.

laura

Disclaimer: My qt is rather rusty, but the signal/slot mechanism is still just C++ function calls. If the signal/slot mechanism actually copies objects into internal storage, my apologies (you'll need to check the Qt pages, there's a big one on signals/slots afaik) - as the bits below will only be relevant in a C++ context, not in a C++ + Qt context.

If you leave out the reference, the string will be copied (and having the const would not matter, any changes made to it will remain in the function alone).
If you leave in the reference but take out the const, you allow the method to modify the string that you give it. They both work, but do different things to the object you pass (amount of copying/possibility of retaining changes).

I suggest you read the following resources:

(on const correctness) https://isocpp.org/wiki/faq/const-correctness

(on references) https://isocpp.org/wiki/faq/references

to understand exactly what passing a parameter is and how void foo(const A&)/ void foo(const A)/ void foo(A&)/ void foo(A) are all different.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!