Const member functions in C++
A function becomes const when const keyword is used in function’s declaration. The idea of const functions is not allow them to modify the object on which they are called. It is recommended practice to make as many functions const as possible so that accidental changes to objects are avoided.
Following is a simple example of const function.
#include<iostream> using namespace std; class Test { int value; public: Test(int v = 0) {value = v;} // We get compiler error if we add a line like "value = 100;" // in this function. int getValue() const {return value;} }; int main() { Test t(20); cout<<t.getValue(); return 0; } |
chevron_right
filter_none
Output:
20
When a function is declared as const, it can be called on any type of object. Non-const functions can only be called by non-const objects.
For example the following program has compiler errors.
#include<iostream> using namespace std; class Test { int value; public: Test(int v = 0) {value = v;} int getValue() {return value;} }; int main() { const Test t; cout << t.getValue(); return 0; } |
chevron_right
filter_none
Output:
passing 'const Test' as 'this' argument of 'int Test::getValue()' discards qualifiers
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Some interesting facts about static member functions in C++
- Difference between const char *p, char * const p and const char * const p
- Can C++ reference member be declared without being initialized with declaration?
- Count the number of objects using Static member function
- Can we access private data members of a class without using a member or a friend function?
- “static const” vs “#define” vs “enum”
- Function overloading and const keyword
- Why copy constructor argument should be const in C++?
- Functions in C/C++
- C++ Mathematical Functions
- Inline Functions in C++
- Pure Functions
- List in C++ | Set 2 (Some Useful Functions)
- Searching in a map using std::map functions in C++
- Functions that cannot be overloaded in C++



