Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rules for Using References in C++ [duplicate]

Tags:

c++

Possible Duplicate:
How to pass objects to functions in C++?
Why pass by const reference instead of by value?

In C++ is there a kind of rule or guidance of when exactly one must or at least should opt for using pass by references than by value?

And if is about the size of the object with some object as far as know (little honestly) it can become difficult to judge.

like image 868
Amani Avatar asked Dec 19 '12 07:12

Amani


1 Answers

You should pass by reference whenever you want changes to the arguments to be visible outside the function. This is a pass by non-const reference and is mostly motivated by logic rather than efficiency.

You should pass by (mostly const) reference when you're dealing with data types with size larger than that of a register. Since the reference is mostly const, you can't make any changes to it, so this is motivated by efficiency.

like image 137
Luchian Grigore Avatar answered Sep 19 '22 15:09

Luchian Grigore