0

While viewing some code I came across a method with the following parameter

methodName(const std::string & parameterName)

I know const is used for a value that can't change in the method, and & is a value of a reference.

Why and when should you use this and not just a const string?

Sven van den Boogaart
  • 11,833
  • 21
  • 86
  • 169
  • Note: the flavour 'void f(const T)` without reference is just an implementation detail (preventing modifications on a copied argument) –  Nov 23 '15 at 19:36
  • What book do you read that does not cover such a basic thing? – Christian Hackl Nov 23 '15 at 19:38

4 Answers4

8
  • const is an assurance that the function will not change the argument.

  • & is a reference; that means that the string passed to methodName is passed by reference. This has the advantage that it is not copied and therefore improves performance.

In combination, it's a common pattern for passing big objects efficiently. A simple const std::string would copy the whole string, which, for long strings, would be pretty inefficient.


Since C++11, you can also pass objects efficiently by moving them.

Community
  • 1
  • 1
cadaniluk
  • 15,027
  • 2
  • 39
  • 67
1

I know const is used for a value that can't change

const is used for anything that can't change - a value, a pointer, or a reference. It is a good idea to use const in situations when something is not supposed to change, even if you pass it by value.

Why and when should you use this and not just a const string?

const string& is more economical, because it lets you avoid copying the content of the string. You should use it when the string that you are passing to the function is guaranteed to remain in place for the duration of the function. This is always the case when you call a function directly.

In contrast, const string should be used when there is a possibility that the string could be deleted while the function is still running.

For example, this could happen when you call a function on a different thread, and then delete the parameter that you passed to it, or let the parameter go out of scope. Trying to pass the parameter by const reference would result in undefined behavior, while passing it by const value would make a private copy, thus preventing UB.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

Notwithstanding all answers here, sometimes it is actually preferable to pass strings by value, and that would be specifically for performance gains. A most common example would be an implementation of operator+ for strings.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
0

you need to use const std::string instead of const string, because you are using the C++ String Class which is not accessible to your program globally.

This is because the C++ String Class is defined under the namespace std (namespace is a set of classes), and to use the C++ String Class you will need to access that from that namespace. Also, you would need to use scope resolution operator (::).

syntax:

<namespace_name>::<class_name>

Hence you need to use std::string.

user3071284
  • 6,955
  • 6
  • 43
  • 57
Gaurav
  • 111
  • 3