| 2 AM coding mistake of the day |
[21 Jun 2002|11:06am] |
Am I the only one who has inflicted more bugs by using the const keyword than by omitting it? I know it's supposed to catch coding bugs by preventing modifications to data that shouldn't be touched, but what of when it actually introduces an unexpected bug? The story goes as such:
I realized partway through a project involving plenty of inheritance and virtual functions that I had not bothered to const any of the simple functions that don't modify their class (ie "int getSomething() const;"). When I const-ed a virtual function in a base class, I forgot to also const it in a derived class. You'd think the compiler would catch this, but gcc must have just decided that the two functions were valid as completely unrelated functions. The worst part is that everything compiled fine, but the derived function would never get called -- only the const-ed ancestor. All this thoroughly confused gdb (the debugger), making this a hard-to-crack bug.
Anyone else have similar experiences with const? I'm not going to give up on the keyword just yet, but my faith in its helpfulness has been reduced somewhat.
|
|