Note that there are some explanatory texts on larger screens.

plurals
  1. PODoes it make sense to use const in an interface or not?
    primarykey
    data
    text
    <p>I have a module that performs some calculations and during the calculations, communicates with other modules. Since the calculation module does not want to rely on the other modules, it exposes an interface like this (this is a very simplified version of course):</p> <pre><code>class ICalculationManager { public: double getValue (size_t index) = 0; void setValue (size_t index, double value) = 0; void notify (const char *message) = 0; }; </code></pre> <p>Applications that want to use the calculation module need to write their own implementation of the interface, and feed it to the calculation tool, like this:</p> <pre><code>MyCalculationManager calcMgr; CalculationTool calcTool (calcMgr); calcTool.calculate(); </code></pre> <p>I am wondering now whether it makes sense to add "const" to the methods of the ICalculationManager interface.</p> <p>It would seem logical that the getValue method only gets something and doesn't change anything, so I could make this const. And setValue probably changes data so that won't be const. But for a more general method like notify I can't be sure.</p> <p>In fact, for none of the methods I can now for sure that the method is really implemented as a const method, and if I would make the interface methods const, I am forcing all implementations to be const as well, which is possibly not wanted.</p> <p>It seems to me that const methods only make sense if you know beforehand what your implementation will be and whether it will be const or not. Is this true?</p> <p>Doesn't it make sense to make methods of this kind of interface const? And if it makes sense, what are good rules to determine whether the method should be const or not, even if I don't know what the implementation will be?</p> <p><strong>EDIT:</strong> changed the parameter from notify from "char *" to "const char *" since this lead to irrelevant answers.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload