Who`s Afraid to Be Const Correct? Reading Const Correctly in C++ - Right There Right Now
(Page 6 of 6 )
Don’t be tempted to implement const-correctness at a later stage when you can do it right now. Making a parameter const in a function declaration can have a cascading effect. It is very likely for variables and objects to be passed on from function to function, and a stricter contract near the top of the call-stack can have deep-reaching consequences.
When you have these functions:
void foo(int &val) { bar(val); }
void bar(int &val) { g(val); }
void g(int &val) { … }
Changing the non-const reference parameter ‘val’ to a const reference will affect the other two functions as well. When you are in full production… it will only be the beginning of a long and painful code correction.
What better day to start than today. Delay will only make it more painful, and not doing it won’t help you write better code.
References [K&R] – Kernighan and Ritchie
“The C Programming Language” – ISBN 0131103628
[Meyers] – Scott Meyers
“Effective C++” – ISBN 0201924889
item 20 ‘Avoid data members in the public interface’
item 21 ‘Use const whenever possible’
[Sutter] – Herb Sutter
“Exceptional C++” - ISBN 0201615622
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |