Const Member Function in C++
This article is about the use of constant member functions in C++. We use the keyword const
to make any object, variable, or function constant.
Like any other variable or object, member functions of a class can also be made const
. Certain rules and regulations for such functions will be discussed in this article.
Const
Member Function in C++
Const
is a type qualifier, a keyword used with a data type indicating that the data is read-only. While it can be used to declare constants, Const
varies from comparable constructions in other languages in that it is part of the type and so has complicated behavior when paired with pointers, references, composite data types, and type-checking.
Const
member functions are declared as constants in the program. The object referred to by these functions cannot be changed.
It’s advisable to use the const
keyword to avoid unintentional changes to the object. Any object can call a const
member function.
Only non-const
objects can call non-const
functions. If we declare the object of a class as a const
object, it can call only const
member functions, whereas a non-const
object can call all the member functions const
or non-const
.
The syntax for making a const
function is:
return -datatype function_name() const;
Consider the example below.
#include <iostream>
using namespace std;
class Data {
int val;
public:
Data(int a = 0) { val = a; }
int getValue() const { return val; }
};
int main() {
Data d(10);
cout << d.getValue();
return 0;
}
We have declared a class Data
with a constructor and a member function getValue()
in this code segment. This function is a const
function so that it cannot change the value of the data member.
If it modifies the data of any data member, the compiler will generate an error. The this
pointer in this function is a pointer to a const
object provided to a const
member function.
This ensures that the pointer cannot be used to modify the object’s data members. Any effort to modify a data member of an object called a constant method and any attempt to call a non-const
member function for that object will result in a syntax error.
A constant member function can have a non-constant version overloaded. The compiler determines which version to use based on whether it is called by a const
object or a non-const
object.
Const
constructors and destructors can never be declared. They can always change a data member, even if the object itself is constant.
Static member functions cannot be defined to be const
. The const
keyword affects the this
pointer reference provided to a member function, but static member functions do not have one because they can be called without an object.
It is also possible to relax the const
function limitation that prevents the function from writing to any class variable. These class variables are marked with the mutable
keyword to allow them to be writable even when the function is marked as a const
function.
If a class variable is marked as mutable
and a const
function writes to it, the code will compile correctly, and the variable can be changed (C++11).
Changing the placement of the const
keyword in a C++ statement has completely distinct semantics, as is typical when working with the const
keyword. The preceding const
usage only applies when const
is placed at the end of the function declaration after the parentheses.
Husnain is a professional Software Engineer and a researcher who loves to learn, build, write, and teach. Having worked various jobs in the IT industry, he especially enjoys finding ways to express complex ideas in simple ways through his content. In his free time, Husnain unwinds by thinking about tech fiction to solve problems around him.
LinkedIn