Question
What `const` Means at the End of a Member Function in C++
Question
In C++, what does const mean when it appears at the end of a member function declaration, such as in the following example?
class foobar
{
public:
operator int() const;
const char* foo() const;
};
I want to understand what that trailing const does, how it affects the function, and what it means for the object on which the function is called.
Short Answer
By the end of this page, you will understand what a const member function is in C++, why const at the end of a method matters, how it changes the type of this, and when you should use it. You will also see examples, common mistakes, and how this concept appears in real C++ codebases.
Concept
In C++, when const appears after a member function declaration, it means the function is a const member function.
class Example {
public:
int getValue() const;
};
This tells the compiler that the function does not modify the object's observable state.
What it really means
Inside a non-static member function, C++ automatically passes a hidden pointer called this, which points to the current object.
- In a normal member function,
thisis effectively:
Example* const this;
- In a
constmember function,thisbecomes:
const Example* const this;
That means inside the function:
- you cannot modify data members of the object
- you cannot call non- member functions on the same object
Mental Model
Think of an object like a book in a library.
- A non-const member function is like writing notes in the book.
- A const member function is like reading the book without changing it.
If a function ends with const, you are telling the compiler:
"This function only reads from the object. It does not edit it."
Another useful mental model:
int getAge() const= asking a questionvoid setAge(int x)= changing something
So in practice:
- getters are usually
const - setters are usually not
const
The compiler acts like a strict librarian: if you said a function is read-only, it will stop you from changing the object inside that function.
Syntax and Examples
Basic syntax
class Person {
public:
int getAge() const;
void setAge(int value);
private:
int age;
};
Here:
getAge() constpromises not to change the objectsetAge(int value)is allowed to change the object
Example implementation
#include <iostream>
using namespace std;
class Person {
public:
Person(int a) : age(a) {}
int getAge() const {
return age;
}
void setAge( value) {
age = value;
}
:
age;
};
{
;
cout << p.() << ;
p.();
cout << p.() << ;
}
Step by Step Execution
Traceable example
#include <iostream>
using namespace std;
class Counter {
public:
Counter(int v) : value(v) {}
int getValue() const {
return value;
}
private:
int value;
};
int main() {
const Counter c(10);
cout << c.getValue() << '\n';
}
Step by step
1. const Counter c(10);
A Counter object is created with value = 10.
Because c is declared as const, you can only call const member functions on it.
Real World Use Cases
Read-only accessors
A very common use is getter functions:
std::string getName() const;
int size() const;
bool empty() const;
These functions inspect an object without changing it.
Working with const references
Large objects are often passed by const reference to avoid copying:
void printUser(const User& user) {
std::cout << user.getName() << '\n';
}
For this to work, getName() must be a const member function.
Standard library style
The C++ standard library uses const member functions heavily:
std::string::size() const
Real Codebase Usage
In real C++ projects, trailing const is part of writing const-correct APIs.
Common patterns
Getters are usually const
class Config {
public:
std::string getHost() const;
int getPort() const;
};
Validation helpers are const
class Email {
public:
bool isValid() const;
};
Query methods are const
class Buffer {
public:
std::size_t size() const;
bool empty() const;
};
Common Mistakes
1. Confusing return-type const with member-function const
These are different:
const char* foo() const;
const char*applies to the returned pointer target- trailing
constapplies to the object
A beginner may think they mean the same thing, but they do not.
2. Forgetting to mark getters as const
Broken example:
class User {
public:
int getId() {
return id;
}
private:
int id;
};
void printUser(const User& u) {
// u.getId(); // compile error
}
Fix:
class {
:
{
id;
}
:
id;
};
Comparisons
Trailing const vs other uses of const
| Syntax | Meaning |
|---|---|
const int x = 5; | x cannot be changed |
const char* p; | the characters pointed to by p should not be modified through p |
int* const p = &x; | p itself cannot point somewhere else |
int get() const; | this member function does not modify the object |
Const member function vs non-const member function
| Feature | Non-const function |
|---|
Cheat Sheet
Quick rule
ReturnType functionName() const;
Trailing const means:
- the function does not modify the object
- it can be called on
constobjects - inside the function,
thisis treated like a pointer to const
Common examples
int size() const;
bool empty() const;
std::string name() const;
operator int() const;
Your example
class foobar {
public:
operator int() ;
;
};
FAQ
What does const after a member function mean in C++?
It means the function promises not to modify the object it is called on, except for mutable members.
Can a const member function be called on a non-const object?
Yes. A const member function can be called on both const and non-const objects.
Why do I get an error calling a method on a const object?
Because the method is not marked const. The compiler only allows const member functions to be called on const objects.
Is const char* foo() const the same const twice?
No. const char* makes the returned characters read-only through the pointer. The trailing const means the function does not modify the object.
Can a const member function change anything at all?
Yes, it can modify members marked mutable. It can also change external objects through pointers or references if your code allows it, but it should not change the logical state of the object itself.
Should every getter be const?
Usually yes. If a getter only reads data, it should normally be marked const.
What is const correctness in C++?
Mini Project
Description
Build a small Book class that stores a title and page count. This project demonstrates how const member functions let you safely read object data, especially when working with const objects and const references.
Goal
Create a class with read-only accessor methods marked const, and verify that they can be called on a const object.
Requirements
- Create a
Bookclass withtitleandpagesdata members. - Add a constructor to initialize both values.
- Add two getter methods marked
const. - Write a function that accepts a
const Book&and prints the book details. - Create a
const Bookobject inmainand call the getter methods.
Keep learning
Related questions
Basic Rules and Idioms for Operator Overloading in C++
Learn the core rules, syntax, and common idioms for operator overloading in C++, including member vs non-member operators.
C++ Base Class Constructor Rules Explained
Learn how C++ base class constructors are called from derived classes, including order, syntax, defaults, and common mistakes.
C++ Casts Explained: C-Style Cast vs static_cast vs dynamic_cast
Learn the difference between C-style casts, static_cast, and dynamic_cast in C++ with clear examples, safety rules, and real usage tips.