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?

2
  • Note: the flavour 'void f(const T)` without reference is just an implementation detail (preventing modifications on a copied argument) Commented Nov 23, 2015 at 19:36
  • What book do you read that does not cover such a basic thing? Commented Nov 23, 2015 at 19:38

4 Answers 4

9
  • 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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.