What does const mean at the end of a class member function declaration in C++?
Hello colleagues! I started learning C++ not long ago and encountered syntax that is completely unclear to me. I see in code class method declarations where the keyword `const` appears after the parameter list. For example:
```cpp
class MyClass {
public:
int getValue() const; // What does this const mean?
void setValue(int v);
};
```
I understand that `const` before a variable type makes it immutable (for example, `const int x = 5;`). But what does `const` at the end of a function declaration do? I tried to write such a method, but when I tried to modify a class field inside it, the compiler gave an error. Why?
Here is my code that does not compile:
```cpp
class Counter {
private:
int count = 0;
public:
int getCount() const {
count = 10; // Error: cannot modi
No answers yet.
Log in to leave an answer
Blogs
Functional Programming in Rust: Idioms and Practices