TransWikia.com

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

Stack Overflow Asked by IvanaGyro on December 13, 2020

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?
}

One Answer

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.

Correct answer by tadman on December 13, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP