C++ and QT4.5 - is passing a const int& overkill? Does pass by reference helps in signals/slots?

前端 未结 3 896
-上瘾入骨i
-上瘾入骨i 2021-02-15 16:02

Two questions rolled into one here...

I have a number of functions which are called multiple times per frame for a real-time video processing application. Taking advice

3条回答
  •  -上瘾入骨i
    2021-02-15 16:17

    Yes, this is overkill and will actually result in slower code than if you passed the int by value. An int is four bytes; a reference (essentially a memory address) is either also four bytes (on a 32-bit machine) or eight bytes (on a 64-bit machine). So you may actually need to pass more information to the function -- and additionally, you have the overhead of dereferencing that reference.

    If you're passing something bigger than an int, however, it's more efficient to use a const reference, because you can pass just four or eight bytes instead of having to copy the whole object.

    Edit Regarding Qt: Yes, if the slot takes a const reference to an object, then the reason for that is to save the overhead of copying the object.

提交回复
热议问题