Search⌘ K

Ignoring the const

Explore when the const keyword is ignored in C++ function parameters and why it does not create new overloads for non-reference types. Understand the importance of matching function declarations and definitions to avoid confusion and maintain clear, safe code.

We'll cover the following...

When is const ignored?

In the section about const functions, we saw that the following signatures are declared two different overloads.

We can overload a function based on its constness.

C++
#include <iostream>
class A {
public:
void foo(){
std:: cout<<"Non const"<< '\n';
}
void foo() const{
std:: cout<<"const"<< '\n';
}
};
int main() {
A a;
a.foo();
}

On the other hand, any const qualifier ...