8

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.

Community
  • 1
  • 1
Amani
  • 16,245
  • 29
  • 103
  • 153

4 Answers4

7

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.

Peter G.
  • 14,786
  • 7
  • 57
  • 75
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Passing by value is often faster than reference even for larger objects because lack of `restrict`. – Pubby Dec 19 '12 at 08:39
  • Note: even for a small data type, the presence of a hand-crafted copy constructor should encourage you to use a `const&`. The idiomatic example is the `shared_ptr`, it's not as lightweight as many people think. – Matthieu M. Dec 19 '12 at 08:41
3
  1. If you don't want to invoke copy constructor of the object (for example, if doing so is expansive due to object size).
  2. When you actually need the reference, and not only the value (swap(a,b), for example - since you actually need the original references in order to make the swap.)
amit
  • 175,853
  • 27
  • 231
  • 333
3

Normally, I use pass by reference in following situations:

1) The objetc is bigger size than a pointer: In this case, if I dont want not modify the objetc, use a const reference:

Reference object;

foo(object);

with foo declared as:

void foo(const Reference & object);

2) For code clarify, trying to avoid thigs like this:

int f1 = xxx;
int f2 = yyy;
int f3 = zzz;
....

foo(f1, f2, ............)

using an struct:

struct X
{
    int f1;
    int f2;
    int f3;
    ....
};

X x;
x.f1 = xxx;
.....

foo(X);

with foo defined as:

foo(const X & x);

3) for speedup pourposes: Remember that calling functions must put parameters on stack 4) You need to modify parameters inside the function.

Reference object;

foo(object);

with foo declared as:

void foo(Reference & object);
Tio Pepe
  • 3,071
  • 1
  • 17
  • 22
1

Prefer pass-by-reference-to-const to pass-by-value. Pass by value is an expensive operation because copies of the object are produced by copy costructors. Also, if you have some non-primitive data members in the class (eg:std::string), every time a new object of the class is created, those std::string objects also would be created.

Passing by reference also avoids slicing problem. If you pass a derived class object by value to a function expecting base class object, the derived features of the derived class would be sliced off and the object inside the function would behave as a base class object. If you pass by reference, slicing can be avoided.

Nevertheless, there are some cases where you cannot pass by reference. If your function arguments or return value needs to have NULL values, you cannot use references.

Sanish
  • 1,699
  • 1
  • 12
  • 21