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

前端 未结 3 893
-上瘾入骨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条回答
  •  醉酒成梦
    2021-02-15 16:23

    First - the difference between

    • void processSomething(const int& value);
    • void processSomething(int value);

    is: normally a const reference is passed by passing a pointer, the other thing is passed by copying. After the call (from the caller side) the result is equal. Whatever you pass to the function is not changed by the call.

    Inside the function you won't see any differences either (at least on an int). On Objects, you may only use const functions of course.

    On the performance side - passing a const reference to an int may (or may not) be slower, depending on compiler and optimization. The compiler could (in theory) optimize the passing of the const reference away to a pass by value, I don't know if it does though. Working with pointers to ints instead of values is slower of course.

    For more information on that see: When to use pointers, and when not to use them


    After reading the other linked post - the compiler can not optimize it away in a multithreaded environment. At least in a MT-environment there are real differences betwenn const int& and int calls. +1 for that link.

提交回复
热议问题