I am reading something about C++, and it says "Methods that never change a class member value should be declared as constant using the const keyword in both the declaration and definition. This should be inserted just after the closing argument parenthesis and helps to prevent errors." Then, in its code example, is has
I have never heard of this, nor even knew you could do such a thing, and thus obviously never do so in my code.Code:class Dog { void bark() const {cout << "WOOF!\n"; }; };
Why would you do this? How does it prevent errors as the quote claims? Does anyone else do it? It just seems like silly, extra baggage to me.