1

Does it makes sense to use the const qualifier for input parameters in C++ when the parameter is passed by value ?

For example if I pass a primitive type like integer:

void function( const int aValue );

does it gain any benefit over the same function without using the const:

void function( int aValue );

Is there any difference if I pass my own data type ?

void function( Data aData );
void function( const Data aData );
p.i.g.
  • 2,815
  • 2
  • 24
  • 41

2 Answers2

4

Since you are passing by value const does nothing for you in this case in terms of stopping you from modifying the original object. It does stop you from modifying the copy in the function though.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
2

It means that the function body cannot modify the parameter. So it does give you a little more program stability.

The same rule applies for your own classes, although you might want to pass those by const reference if a value copy is expensive.

Some houses insist on the style: const unless non-const is necessary.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483