Pass by Value and Pass by Reference

后端 未结 2 451
感动是毒
感动是毒 2021-01-22 14:22

I\'m working on a project that calculate income for employees by using overloading and also pass by value + pass by reference. I need to use at least one of the functions in pro

2条回答
  •  抹茶落季
    2021-01-22 15:11

    Here are some guidelines:

    • Pass by value if the value can fit into the processor's registers and the parameter will not be modified.
    • Pass by reference if the parameter will be modified by the function.
    • Pass by constant reference if the object is larger than the processor's register and the parameter will not be modified.

    Passing by constant reference for types like double, float, int, char, and bool doesn't make sense because these usually fit into the processor's word size. The compiler will try to pass these values in a register. These POD types have no additional copying costs. Thus, pass by value (less typing required).

提交回复
热议问题