What is the use of "const" functions? [duplicate]
This question already has an answer here:
what does const mean in c++ in different places 3 answers
Functions returning a const value [duplicate] 1 answer
This code looks weird:
const double square(double x) {
return x*x;
}
Common sense tells me that const in this context means either
the function returns a const double
OR
it promises not to change the double that was passed in. But it was passed
in by value! I don't understand
OR
I know that const member functions promise not to change *this, but this
is not even a member function.
Edit
What's the point to return a const double if you can save the result in a
non-const variable and edit it??
double var = square(4.5); // no compile error
var = 0.3;
No comments:
Post a Comment