c++ Which is faster, returning a value in a function, or using a reference parameter? -


aside what's better or secure practice, faster return value in function, or change parameter reference? or there faster method?

a big object deep copy slow return value if rvo doesn't kick in.

but size not play. example, fast return 1 byte object return 2 or 4, or 8 byte object, depending on platform.

quite not faster, needed in usage context. in case of primitive , when intent output of value, returned value. when object identity matter or intent modification of actual object, return reference.

there limitation of possible. example, should never return function local reference, because that's problem waiting happen. return private member variable reference, again, have public instead.

it depends on mean "return reference" because c++ style returning reference, or c style returning pointer. latter more used, when dynamically allocate object in function , return pointer it, in case of big or unique objects way go.

also, besides rvo there move semantics, still come play when returning value. overhead of deep copies long standing issue resolved in c++11. frameworks qt solved issue earlier employing shallow copies , using implicitly shared resources.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -