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.

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 graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

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

How To Learn The Move Constructors In Modern C++?

C++C++17Code SnippetLearn C++

How To Convert Char Array String To Unicode String Correctly In C++?

Artificial Intelligence TechC++Language FeatureLearn C++

How To Develop A Simple Hopfield Network In C++

Worth reading...
Learn About C++ Functions