FAQ 14.06 Why should const correctness be done sooner rather than later?

FAQ 14.06 Why should const correctness be done sooner rather than later?

Adding constraints later can be difficult and expensive. If a function was not originally restricted with respect to changing a by-reference parameter, adding the restriction (that is, changing a parameter from string& to const string&) can cause a ripple through the system. For instance, suppose f() calls g(), and g() calls h():

 #include <string> #include <iostream> using namespace std; void f(string& s) throw(); void g(string& s) throw(); void h(string& s) throw(); int main() {   string s;   f(s); } void f(string& s) throw() { g(s); } void g(string& s) throw() { h(s); } void h(string& s) throw() { cout << s << '\n'; } 

Changing f(string& s) to f(const string& s) causes error messages until g(string&) is changed to g(const string&). But this change causes error messages until h(string&) is changed to h(const string&), and so on. The ripple effect is magnificent and expensive.

The moral is that const correctness should be installed from the very beginning.



C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net