- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Meaning of 'const' last in a function declaration of a C++ class?
Sometimes we can find the keyword ‘const’ present at the last of function declaration. So what does it mean?
Using this one function can be made as constant. The idea of constant function is that, the function cannot be modified from the objects, where they are called. It is recommended to use the constant functions in our program.
Let us see one example of constant function.
Example
#include<iostream> using namespace std; class MyClass { int value; public: MyClass(int val = 0) { value = val; } int getVal() const { //value = 10; [This line will generate compile time error as the function is constant] return value; } };
Output
The value is: 80
Now we will see another important point related to constant functions. The constant functions can be called from any type of objects, as you have seen from the above example. But some non-constant functions cannot be called from constant objects.
Example
#include<iostream> using namespace std; class MyClass { int value; public: MyClass(int val = 0) { value = val; } int getVal(){ return value; } }; main() { const MyClass ob(80); cout<< "The value is: " << ob.getVal(); }
Output
[Error] passing 'const MyClass' as 'this' argument of 'int MyClass::getVal()' discards qualifiers [-fpermissive]
- Related Articles
- What's the meaning of the phrase 'Once in a blue moon'?
- How to use 'const' keyword in JavaScript?
- Print a number as string of 'A' and 'B' in lexicographic order in C++
- Why is 'class' a reserved word in JavaScript?
- Standard Size of character ('a') in C/C++ on Linux
- Fermat's Last Theorem in C++
- Explain 'Not a constructor function' error in JavaScript?
- What's the meaning of the phrase 'what you seek is seeking you'?
- Update 'a' record with 'b' and 'b' with 'a' in a MySQL column (swap) with only 'a' and 'b' values?
- How to invoke a JavaScript Function with 'new' Function Constructor?
- Program to find number of string we can make where 'a' can be 'a' or 'b', and 'b' remains 'b'in Python
- What is the meaning of the phrase 'the use of the right vocabulary' for a writing task?
- What is the meaning of the shloka 'Guru Bramha Guru Vishnu'?
- When should I write the keyword 'inline' for a function/method in C++?
- Finding the sum of two numbers without using '+', '-', '/', '*' in JavaScript

Advertisements