Here’s the snippet in title (more readable) bool foo() const { return bar; }?
If not, is it different from const foo() {return bar;}? Or is it the same thing as saying the argument the function takes is a constant?
I’ve tried scouring cppreference and can’t find anything.
>Solution :
The const in the first code snippet:
bool foo() const { return bar; }
Means that the method does not modify the instance of the class that owns the method. Let’s add some more code to make this clear:
bool bar = true;
struct Foo {
bool foo() const { return bar; }
};
int main() {
const Foo f;
f.foo();
return 0;
}
When f.foo() is called the instance f is not modified. That is the meaning of the const in your snippet. It has nothing to do with the return value bar
The second code snippet is a compiler error. It would return a const foo if it had an identifier