Item 53: Compilation warning
Item 53: Pay attention to compiler warnings.
Compilation warning is very important in C ++ because it may be an error! Do not ignore the warnings, because the compiler author knows more about what the code is. So,
- Take all the warnings seriously and pursue the highest level of warning-free code;
- But don't rely on warning. Maybe another compiler may not have some warning.
In most project practices, it is also common practice to eliminate not only all compilation warnings, but also all code style check warnings.
Let's look at a common error. The compiler will help you find it. For example, we wantDRewriteBVirtual functions inf():
class B{public: virtual void f() const;};class D:public B{public: virtual void f();};
We forgot to writeconstNow! This is not to rewrite a virtual function, but to define a function with the same name and completely hide the function in the parent class.void f() const. Therefore, the compiler will give a warning;
warning: D::f() hides virtual B::f()
The compiler meansBHas not declared suchf. But many ignorant programmers will think: Of courseD::fHiddenB::fThat's what I want! Instead, I forgot to write it myself.const. The errors made here may lead to debugging for a long time, because you have ignored a problem that the compiler has long found.
When you have a lot of experience, you can identify what those warnings are talking about, but the best habit is to eliminate warning. This is because serious problems are easily ignored when the warning blowout occurs. At least when you ignore a warning, make sure that you fully understand what it is talking about.