Effective C + + clause 53

Source: Internet
Author: User

Do not neglect the compiler's warnings

Compiler warnings don't bother some programmers. They believe that warnings do not threaten the program or do not have much of a side effect on the program. However, this idea may cost a great price when it comes to programming.
Here's an example:

class B{    public:        virtualvoidfconst;    };    public B{    public:        virtualvoidf();    };

The programmer of this code wants class D to inherit the F () function of Class B, but it is not declared as const in D. The compiler will not make an error, and may give the following warning:

warningD::f()hidesvirtualB::f()

You might think that's what we want to do. But the compiler is trying to tell you that B::f is not re-declared in D, but is obscured. If you ignore this warning, such as causing error behavior. Polymorphism can be compromised at this time.
The following code:

#include <iostream>using namespace STD;classb{ Public:Virtual voidF ()Const{cout<<"Class B"<< Endl; };};classB | Publicb{ Public:Virtual voidF () {cout<<"Class D"<< Endl; };};intMain () {B *b;    B B1;    D D1;    b = &b1; B->f ();//Call Class B's FD1.F ();//Call Class D's Fb = &d1; B->f ();//Call Class B's F    return 0;}

Learn from compiler warnings that learning about warning messages is not the same as what they seem to mean. Programs that do not have any warnings at the highest warning level are ideal, but have a deep understanding of warning, and you can choose to ignore certain warning messages. But remember, before ignoring this warning, be sure to understand its true intentions.

Warning messages are related to the compiler, and different compilers have different warning standards. For example, the function in the above code obscures the compiler in another compiler, and there may be no warning.

Effective C + + clause 53

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.