About const-correctness

Just wanted to share this, in case anybody would be wondering why const-correctness is important. Here is some code I wrote this morning:

void Box::onMouseDown (CPoint &where, const long& buttons){
if ((buttons==kRButton)||(buttons=(kLButton+kShift))) {
...

Now I’m sure you can all spot the bug, maybe I wasn’t fully awake, or something… of course, like every programmer on the planet, I “almost always” write bug-free code… :)
But anyway, the compiler was nice enough to tell me, and reject the offending line:
error C3892: 'buttons' : you cannot assign to a variable that is const

Thank you ! This missing ‘=’ would have caused a really nasty bug, potentially very hard to track down….

Now, if the method had been defined as
void Box::onMouseDown (CPoint &where, long& buttons)

Then guess what… the compiler would have accepted the faulty code without any complaints. At best, you would need to increase warning levels to the maximum to get this:
warning C4706: assignment within conditional expression

So now… how many missing const definitions can you spot in the VST SDK ?

2 Responses to “About const-correctness”

  1. Matt Says:

    Who passes a long by const reference instead of by value? All you get is a possible indirection. Normally you do it for bigger structures so you don’t have to copy them over (which involves a copy constructor call). So basically you do it for everything but the intrinsic types.

    And you can simply

    #pragma warning(error : 4706)

    if you use VS anyway

  2. Big Tick Says:

    The reference for the long is in the VSTGUI API. Why they put a const on the long, but not on the CPoint, is beyond my understanding….

Leave a Reply


  • What users say

    "If there is love at first sound, this is Rhino! I want it!" - dhrose, USA
  • Register

  • Welcome !

    Welcome to Big Tick web site ! Please login or register.

    Registration is free, and will enable you to download additional instruments and effects.