What it means exactly, that a function is const? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What it means exactly, that a function is const?

it is easier to me understand with a variable, or object, so they cannot be changed. but a function? it means, that the function cannot be modified, or the function doesnt modifes?

30th Nov 2016, 9:36 PM
Benedek Máté Tóth
Benedek Máté Tóth - avatar
4 Answers
+ 4
Yeah it doesn't help that C++ often uses the same name for not the same thing. If a function of a class is 'const', it cannot change any member of the object it lives in. You can read that as "after calling the function, everything is as it was before". This is not only useful for the programmer (you know that the object didn't change while the function did it's thing) but also because the compiler can do optimizations this way. Also, if you have a const object, you can only call it's const functions - as you said, const objects cannot be changed, and functions often change things, so that's disallowed. If the function is const however, you know it will not change things, so it's fine. "const-correctness" is important, and as a rule of thumb, make everything you can const! The next step up is constexpr but I won't go into that. Have a look at it some time :P
30th Nov 2016, 10:12 PM
Schindlabua
Schindlabua - avatar
+ 2
No worries. C++ is one of the few languages that do this, yes! C++ is all about having as much control of your code as possible. If you are just starting out it might not be obvious, but it turns out that a lot of things you write can be made const. I would say at least half of the functions I write are const! I don't think I have any meaningful examples, but the next time you are programming something, just try it - make every function const and remove the const only if you have to (not the other way around). If you remove the const, maybe also try thinking about whether part of your function can be turned into a second, const, function. You would be surprised how clean your code becomes! Suddenly, every function that changes state is isolated and you have a good idea about how and when your object may change over time. It's something that comes with practice I guess. EDIT: const does not mean you cannot access the other members. A lot of functions only need to read variables, and do not need to change them. class X{ string status; public: // ... bool is_done() const{ return status == "ok"; } };
30th Nov 2016, 10:58 PM
Schindlabua
Schindlabua - avatar
+ 1
thank you, sounds a bit esoteric. so what does the const function? it means, that when i create it, i should not put inside task, that change a value, or it does the calculation, but after reaarrange the state, if you understand what i mean. my english is poor, sorry.
30th Nov 2016, 10:21 PM
Benedek Máté Tóth
Benedek Máté Tóth - avatar
+ 1
oke, thanks for your explications, and advices, i will try this!
1st Dec 2016, 4:07 PM
Benedek Máté Tóth
Benedek Máté Tóth - avatar