Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between const reference and normal parameter

void DoWork(int n); void DoWork(const int &n); 

What's the difference?

like image 247
Pirate for Profit Avatar asked Apr 13 '10 05:04

Pirate for Profit


People also ask

What is the difference between const reference and reference?

A const reference is actually a reference to const. A reference is inherently const, so when we say const reference, it is not a reference that can not be changed, rather it's a reference to const. Once a reference is bound to refer to an object, it can not be bound to refer to another object.

Why should we use const references for function parameters rather than pass by value?

Passing arguments by value and thus copying them can be expensive - const references avoid that expensive step while still promising the caller that the object won't be changed. Usually fundamental types ( int , double , ...) are passed by value, while class-types are passed by const reference.

What is one benefit of declaring the parameter as a const reference?

What is one benefit of declaring the parameter as a const reference instead of declaring it as a regular object? Actually, objects cannot be passed as regular variables, because they require a constructor call. Therefore, a const reference is the only way to pass class instances to functions.

What is const parameter?

A constant parameter, declared by the keyword const , is a read-only parameter. This means that we can not modify the value of the constant parameter in the function body. Using the const keyword tells the compiler that the value of that parameter will not be changed inside the function.


2 Answers

The important difference is that when passing by const reference, no new object is created. In the function body, the parameter is effectively an alias for the object passed in.

Because the reference is a const reference the function body cannot directly change the value of that object. This has a similar property to passing by value where the function body also cannot change the value of the object that was passed in, in this case because the parameter is a copy.

There are crucial differences. If the parameter is a const reference, but the object passed it was not in fact const then the value of the object may be changed during the function call itself.

E.g.

int a;  void DoWork(const int &n) {     a = n * 2;  // If n was a reference to a, n will have been doubled       f();  // Might change the value of whatever n refers to  }  int main() {     DoWork(a); } 

Also if the object passed in was not actually const then the function could (even if it is ill advised) change its value with a cast.

e.g.

void DoWork(const int &n) {     const_cast<int&>(n) = 22; } 

This would cause undefined behaviour if the object passed in was actually const.

When the parameter is passed by const reference, extra costs include dereferencing, worse object locality, fewer opportunities for compile optimizing.

When the parameter is passed by value an extra cost is the need to create a parameter copy. Typically this is only of concern when the object type is large.

like image 60
CB Bailey Avatar answered Oct 03 '22 15:10

CB Bailey


The difference is more prominent when you are passing a big struct/class:

struct MyData {     int a,b,c,d,e,f,g,h;     long array[1234]; }; void DoWork(MyData md); void DoWork(const MyData& md); 

When you use use 'normal' parameter, you pass the parameter by value and hence creating a copy of the parameter you pass. If you are using const reference, you pass it by reference and the original data is not copied.

In both cases, the original data cannot be modified from inside the function.


EDIT:
In certain cases, the original data might be able to get modified as pointed out by Charles Bailey in his answer.

like image 20
Afriza N. Arief Avatar answered Oct 03 '22 16:10

Afriza N. Arief