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!

close

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.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++Introduction to C++Language FeatureLearn C++

How To Use The Comprehensive Form Properties In C++ VCL Apps

C++C++11C++14C++17Introduction to C++Learn C++

This Is How To Add Characters to Wide Strings in Modern C++

C++ComponentsLanguage FeatureLearn C++

How To Create Gorgeous Transparent App Windows With C++

C++C++11C++14C++17Introduction to C++Learn C++

This Is How To Get A Substring of a Wide String in C++

Worth reading...
Learn About Parameters Passed by Reference in Functions
en_USEnglish