Item 53: Compilation warning

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.