'C++ why declare member function const
I understand that specifying a member function "const" allows us to call it from a const object, but why do we have to explicitly write "const" next to the member function? Why can't the compiler figure out that
int getFoo() {
return m_foo;
}
is indeed "const" for all intents and purposes?
Solution 1:[1]
Because C++ allows declarations to be in different places to definitions.
If all you have is
class Bar {
int m_foo;
public:
int getFoo();
};
can you safely call getFoo on a const Bar?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Caleth |
