The const Keyword

The const keyword marks a variable, method or parameter as unmodifiable, and the compiler enforces this at compile time. A const variable cannot be reassigned, a C++ const member function promises not to modify the object's fields, and a const parameter, often passed as const&, promises not to change the caller's data while avoiding a copy.

3 min read

const marks something as immutable, a variable that can't be reassigned, a method that promises not to modify the object it's called on, or a parameter that a function promises not to change. The compiler enforces this at compile time, catching accidental mutations before the code ever runs.

How it works, by context

Const variables: once initialized, the value can never be reassigned.

Const member functions (C++): a method marked const promises it won't modify any of the object's fields, and can be called on const objects (a non-const method cannot).

Const parameters: a function parameter marked const (especially const& for references) promises not to modify the caller's original data, while still avoiding the cost of copying it.

A worked example

#include <iostream>
#include <string>
using namespace std;

class BankAccount {
    double balance;
public:
    BankAccount(double b) : balance(b) {}

    double getBalance() const {      // const method: promises not to modify the object
        return balance;
        // balance = 0;  // this would be a compile error inside a const method
    }

    void deposit(double amount) {    // non-const: this one IS allowed to modify state
        balance += amount;
    }
};

void printAccountInfo(const BankAccount& acc) {   // const&: read-only, no copy needed
    cout << "Balance: " << acc.getBalance() << endl;
    // acc.deposit(100); // compile error, printAccountInfo promised not to modify acc
}

int main() {
    const double interestRate = 0.05;   // const variable, cannot be reassigned
    // interestRate = 0.06;              // compile error

    BankAccount account(1000);
    printAccountInfo(account);
    return 0;
}

getBalance() is marked const because it only reads the object's state, this also means it can be called even on a const BankAccount&, which deposit() (not marked const) could never be called on.

Where this matters in real systems

Passing large objects by const& instead of by value is one of the most common real-world C++ performance practices: it avoids the cost of copying potentially large data (a big string, a big vector) while still guaranteeing to the caller that their data won't be silently modified inside the function. Immutable value types in general, const in C++, final for variables in Java, readonly in C#, are a core building block of thread-safe code, since a value that can never change can be safely shared across multiple threads without locking.

What interviewers actually ask about the const keyword

  • "What does marking a member function const actually guarantee?" That the function will not modify any of the object's non-mutable member variables, and it can therefore be called on const objects and const references.
  • "Why pass a parameter as const& instead of by value?" It avoids the cost of copying the argument (especially for large objects) while still promising the caller that the function won't modify their original data.
  • "Can a const method call a non-const method on the same object?" No, that would violate the const promise, since the non-const method might modify the object.
  • "Is const the same as Java's final?" Close but not identical: const in C++ has more contexts (const methods, const pointers to const data) than Java's final, which mainly just prevents reassignment of a variable or overriding of a method/class.

Key takeaways

  • const marks something as unmodifiable and lets the compiler enforce that at compile time.
  • A const member function promises not to modify the object's state.
  • Passing large parameters as const& avoids copying while still preventing accidental modification.
  • Immutability enabled by const-like keywords is a foundational tool for writing safe, thread-friendly code.
All 72 OOP interview questions

Log in to save which topics you've read across every subject.

Frequently asked questions

What does marking a member function `const` actually guarantee?

That the function will not modify any of the object's non-mutable member variables, and it can therefore be called on const objects and const references.

Why pass a parameter as `const&` instead of by value?

It avoids the cost of copying the argument (especially for large objects) while still promising the caller that the function won't modify their original data.

Can a const method call a non-const method on the same object?

No, that would violate the const promise, since the non-const method might modify the object.

Is `const` the same as Java's `final`?

Close but not identical: `const` in C++ has more contexts (const methods, const pointers to const data) than Java's `final`, which mainly just prevents reassignment of a variable or overriding of a method/class.