Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the "clockwise/spiral rule" to read a const class member function?

Tags:

c++

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?
}
like image 855
IvanaGyro Avatar asked Sep 20 '25 02:09

IvanaGyro


1 Answers

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.

like image 142
tadman Avatar answered Sep 22 '25 02:09

tadman