Reading notes effective C + + Item 53 Follow the compiler-issued warnings

Source: Internet
Author: User

Many programmers often ignore warnings issued by the compiler. After all, if the problem is serious, it will become an error, isn't it? Relatively speaking, this idea may be harmless in other languages, but in C + +, I bet the compiler's implementation has a better understanding of what will happen next. For example, the following error is what everyone commits from time to time:

1 classB {2  Public:3 Virtual voidF ()Const;4 };5 classD: PublicB {6  Public:7 Virtual voidf ();8};

1. Your understanding of the warning may be wrong

Your idea is to redefine the virtual function b::f with D::f, but here's an error: in B, F is a const member function, but in D, F is not declared as Const. I know a compiler will issue the following warning:

Warning:d::f () hides virtual b::f ()

Too many inexperienced programmers interpret the information above as, "of course, D::f hides b::f-and that's what it wants to do!" "But this understanding is wrong. The compiler is trying to tell you that F declared in B is not being re-declared in D; instead, it is completely hidden (as described in Item 33). Ignoring this compiler warning will almost certainly lead to the wrong behavior, and then you will spend a lot of time debugging to find out what the compiler has discovered.

2. Make sure you understand the meaning before ignoring the warning

After you have some experience with a particular compiler's warning message, you will understand what different information means (usually the actual meaning is quite different from what it looks like on the surface). Although it is better practice to write a program without a compiler warning (even at the highest level), once you have experience, you may choose to ignore many warnings. In any case, before you ignore a warning, make sure that you fully understand what the compiler is conveying to you, which is important.

3. Warning messages vary by compiler

Warning messages are related to compilers, so it is not advisable to treat your code carelessly and rely on the compiler to point out your error. For example, the previously mentioned function hides the code, and a different compiler may not have any response.

4. Summary
    • Take the compiler warning seriously and try to write a program without warning at the maximum warning level supported by the compiler.
    • Do not rely on compiler warnings because different compiler warnings warn you about different things. Porting a program to a new compiler might eliminate the warning message you relied on earlier.

Reading notes effective C + + Item 53 Follow the compiler-issued warnings

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.