C++C++11C++14C++17Learn C++

Learn to Use Constant References in C++ Functions

When we call a function with parameters taken by value, it copies the values to be made. This is a relatively inexpensive operation for fundamental types such as int, float, etc. If the parameter is composed of a large compound type, this may result in a certain overhead. For example, let’s consider this following function to combine name, mid name, and surname.

This fullname() function takes three string parameters by their value and returns the result of concatenating them with space characters. In this usage, bypassing the parameters by their value, the function copies given strings values to name, midname, surname parameters. This copy operation happens whenever it is called. If this operation is done multiple times, i.e. sending full names from a long name list to a database, or retrieving from; this means in total it is copying large quantities of data just for the function declaration and call. These data type variables may be also very long strings, structures, bitmaps, etc. This kind of usage may cause your application to run slow or it may operate slower than other applications in the same method or algorithm usage.

This copy can be avoided altogether if both parameters are made references by using & operator. Here we will use a string& instead of string type as below,

As we explained before, parameters by reference do not require a copy. This function operates directly on the string address passed as a parameter, Mostly which means the transfer of certain pointers to the function. In this regard, the version of this fullname() function which uses references is more efficient than the version taking values.

Also, these kinds of functions with reference parameters are generally perceived as functions that modify the arguments passed, because that is why reference parameters are used.

If we want to be sure that these reference parameters are not going to be modified by this function, this can be done by qualifying the parameters as constant. This usage is for the function to guarantee that its parameters can not be modified. Constant parameters can be defined by using the const term, here instead of string& we can use a const string& as in this following example,

This function is forbidden from modifying the parameters by qualifying parameters as const but can still access their values as references (aliases of the arguments) , without having to make actual copies of the strings.

Consequently, a constant reference (const) provides functionality similar to passing arguments by value, but with increased efficiency for parameters of large types. That is why constant parameters are extremely popular in C++ for arguments of compound types. For the most fundamental types, there is no noticeable difference in efficiency, and in some cases, constant references may even be less efficient.

Get started building powerful apps with C++Builder!


Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Learn C++Syntax

What Is A Forced (Default) Copy Assignment Operator In Modern C++

C++C++11C++14C++17C++20Learn C++Syntax

What is Implicitly-declared Copy Assignment Operator In C++?

C++C++11C++14C++17C++20Learn C++Syntax

What is Avoiding Implicit Copy Assignment In C++?

C++C++11C++14C++17C++20Learn C++

Typical Declaration Of A Copy Assignment Operator Without std::swap

Worth reading...
Learn About C++ Functions