Which is more efficient: Return a value vs. Pass by reference?

后端 未结 7 1791
自闭症患者
自闭症患者 2020-11-28 08:37

I am currently studying how to write efficient C++ code, and on the matter of function calls, a question comes to mind. Comparing this pseudocode function:

n         


        
7条回答
  •  隐瞒了意图╮
    2020-11-28 09:02

    This pseudocode function:

    not-void function-name () {
        do-something
        return value;
    }
    

    would be better used when the returned value does not require any further modifications onto it. The parameter passed is only modified in the function-name. There are no more references required to it.


    otherwise-identical pseudocode function:

    void function-name (not-void& arg) {
        do-something
        arg = value;
    }
    

    would be useful if we have another method moderating the value of the same variable like and we need to keep the changes made to the variable by either of the call.

    void another-function-name (not-void& arg) {
        do-something
        arg = value;
    }
    

提交回复
热议问题