How to use the "clockwise/spiral rule" to read a const class member function? Like:
class Box {
// ...
double volume() const; // <= How to read this function?
}
It's not quite the spiral rule here, as that isn't a singular type definition, but a function instead:
double volume() const;
^ ^ ^---- Operates on const instance of Box
| \--- Function name
\-- Return type
Anything with a trailing const
on it can operate on const Box
, while absent that, you need a mutable version or you can't use it. You'll get some kind of compiler error saying it can't find a function for a const Box
even though you'd suppose such a function is defined.
Often you'll see things like:
const myType& getType() const;
Where that returns a const
value from a const
instance. These often end up paired, as in:
const myType& getType() const;
myType& getType();
Where the first one is for read-only access, and the second one allows alteration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With